I am creating a small tile game and I thought it would be fun to instantiate the tiles and store a reference to them in an array on start, but I am having a little trouble. I have tried looking this up, but I am really struggling to search for reinvent information.
In the below code I create an array, instantiate a tile, rename it so it matches the grid address I want to use and then I have tried to put it into the array. But the print functions all return 3-3, so it appears that I have added a reference to my Tile GameObject in my script rather than each array address referencing a different tile. I do intend to pass this array to another function once fixed, but how do I fix this?
public class InstantiateGrid : MonoBehaviour {
public GameObject Tile;
void Start () {
GameObject[,] tileArray = new GameObject[3,3];
for (int i = 1; i < 4; i++) {
for (int j = 1; j < 4; j++) {
Object.Instantiate(Tile);
Tile.name = i + "-" + j;
tileArray[i,j] = Tile;
}
}
print (tileArray[1,1]);
print (tileArray[1,2]);
print (tileArray[1,3]);
}
}