Variable data removed on play.

Okay so, I Have an int array called tTileTypeMap as seen bellow;

[SerializeField]
public int[,,] tTileTypeMap;

I use a function called Create map that i call from a custom editor button and that works fine (I used debugs to check that its there and the data is correct) My code is bellow

public void CreateMap()
{
	if (gridSizeZ <= 0 || gridSizeX <= 0 || MapSize <= 0 || brushes.Count < 0)
		return;

		int y = 0;

		for (int x = 0; x < MapSize; x += gridSizeX)
		{
			for (int z = 0; z < MapSize; z += gridSizeZ)
			{
				tTileTypeMap[x, y, z] = 0;
			}
		}
} 

But when I start play mode, I get a null error and it no longer exists!
Things I have tried so far that don’t work.

  • [SerializeField] - the verible
  • [System.Serializable] - the class
  • Passing the array to a script able object
  • UnityEditor.EditorUtility.SetDirty();

Thank you in advance for any and all help! I have been stuck on this for a hole day!

Okay! I have found a solution, so as it turns out unity can not serialize multi dimensional arrays but it can singe arrays so the working code,
(for this example y is zero because i change the height value else were)

private void ChangeTileTypeMapToFlatTileTypeMap(int[,,] array)
{
	int faltileTypeMapSize = MapSizeX * MapSizeY * MapSizeZ;
	flatTileTypeMap = new int[faltileTypeMapSize];

	int y = 0;

	for (int x = 0; x < MapSizeX; x += gridSizeX)
	{
		for (int z = 0; z < MapSizeZ; z += gridSizeZ)
		{
			flatTileTypeMap[y * MapSizeY + z * MapSizeZ + x ] = array[x, y, z];
		}
	}
} 

So what you have to do is convert the multi dimensional to a “flat file” array using this formula,

[arrayIndex(y) * index-length + arrayIndex (z)* index-length + arrayIndex(x)] = array[x,y,z];

Then to read from it you just do the reverse! Hope this helps someone!