using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameGrid :MonoBehaviour
{
private int width;
private int height;
private int[,] gridArray;
private float cellSize;
[SerializeField]
GameObject tilePrefab;// = default;
public GameGrid(int width, int height, float cellSize)
{
this.width = width;
this.height = height;
this.cellSize = cellSize;
gridArray = new int[width, height];
Debug.Log(width + " " + height);
for(int x = 0; x < gridArray.GetLength(0); x++)
{
for(int z = 0; z < gridArray.GetLength(1); z++)
{
GameObject tile =Instantiate(tilePrefab);
tile.transform.SetParent(transform, false);
tile.transform.localPosition = GetWorldPosition(x,z);
}
}
}
private Vector3 GetWorldPosition(int x, int z, int y = 0)
{
return new Vector3(x, 0, z) * cellSize;
}
}
Thanks very much I'm still new to Unity.
EDIT: Here is my testing.cs
public class Testing : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
GameGrid grid = new GameGrid(20, 10, 1);
}
}
1.Remove the default assignment is not working either. 2.Regarding to the setParent, I do not have a parent, then what parent this child is attached to, if this function will instantiate a parent, then no information is provided to the creation of the parent, still confused.


