- Home /
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!
Answer by RedHeart95 · Dec 20, 2017 at 09:48 PM
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!
Your answer
![](https://koobas.hobune.stream/wayback/20220612144818im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Custom Editor window loses values on play. 1 Answer
SerializedProperty and PropertyField NullReference 1 Answer
Can you force editor scripts to serialize their data,How to serialize fields in an editor script 0 Answers
Serializable settings for custom PropertyDrawer 1 Answer
Is it possible to create a custom gettter/setter on SerializedProperty ? 0 Answers