I have a script that generates a grid of tiles. Each tile is a prefab that contains information that I need to grab later.
script A:
public static Tile[,] _tiles;
I populate the 2D array with my instantiated prefab within a nested For loop.
_tiles[r_i, c_i] = Instantiate(_tilePrefab, new Vector3(c_i, r_i, 0), Quaternion.identity);
In script B which is attached to a button I want to be able to copy the _tile array from script A. So I made a dummy variable and tried to assign the array values to it. rowNum and columnNum are public static variables from script A.
script B:
Public Tile[,] copyA;
Tile[,] copyA = new Tile[A.rowNum,A.columnNum];
for (int x = 0; x < A.rowNum; x++)
{
for (int y = 0; y < A.columnNum; y++)
{
copyA[x, y] = A._tiles[x, y];
}
}
In script A I can verify that the prefab is getting placed in the array _tiles when I set a breakpoint in script A. The error I get after pushing the button for script B is “Object reference not set to an instance of an object”. Somehow my array _tiles is null in script B even though in script A there are Tile objects in it. What am I missing?
As a test, in script A I assigned the prefabs into a public static dictionary. In script B I did var test = A._tiles. I’m able to see my prefabs now. However, for my purposes a dictionary will not work for what I need to do later.