C# Reference of Multidimensional Arrays

As Zerot told me:

I tested and it’s true, we can’t retain reference of a multidimensional array in C#.

public GameObject[,] tileMap = new GameObject[20,20];

All members of tileMap will be null after quitting Play Mode. (For more detailed explanation, check my post)

Are developers aware of this situation? Is any fix planned? Or could at least inform this somewhere in the documentation to avoid losing a few hours thinking why the reference becomes null?

Thanks

You could simply make your own data type to work around this problem…

public class MyGrid()
{
     public List<GameObject> myRows = new List<GameObject>();
     public List<GameObject> myCols = new List<GameObject>();
}

or you could use arrays. I have just found myself using lists more often.

Lists are a bit more dynamic when growing and such. If you made a Editor you could easily traverse this structure.

I would probably choose to create a grid class as a workaround, but I doubt those would be my variables. :slight_smile:

I’d more likely make it a single array, but have math to pull the correct references when using it like a 2 dimensional array. With a little more work, it could probably work with N dimensions as well.

Yeah, workarounds are no problem, but my question was if developers are aware of the situation. But thanks for the alternatives! I personally use the single dimension one, I leave the math for someone interested:

// x, y: 2D array positions you want to access
// width: columns of the 2D array
private int From2Dto1D(byte x, byte y, int width) {
	return width * y + x;
}

Agreed, grid class and a CustomEditor are definitely the way to go if you want proper and productive editor exposure