Undo.RegisterFullObjectHierarchyUndo and inner arrays

Hello. Im trying to make RegisterFullObjectHierarchyUndo with object what have arrays inside.
Everything works right (all fields undo\redo nice), exept this inner arrays. They do not undo and still store “new” values.

    /* Call object's changing function from custom inspector */
    if (GUILayout.Button("Generate") || isJustSpawned) {
    			Undo.IncrementCurrentGroup();
    			Undo.RegisterFullObjectHierarchyUndo(((MapComponent)target).gameObject, "Map generated");
    			((MapComponent)target).Generate(height, width, cellSize);
    			EditorUtility.SetDirty(((MapComponent)target).gameObject);
    		}
    /* Array re-creation in function */
        tilesGrid = null;
		tilesGrid = new GameObject[width, height];
		tilesState = null;
		tilesState = new TileState[width, height];

Here the full code if someone want to read it =)
Custom inspector
and
Object

Uhm the only arrays I can see in your code are multidimensional arrays which Unity can’t serialize at all. So those arrays are never serialized. See the serialization documentation. Search for the section Container field types that can be serialized. Make sure you read the “Note” following that section.

If you want to have your array(s) serialized they need to be one dimensional arrays. You can “sort of” build jagged arrays but not directly. So you can not do GameObject[,] or GameObject[][]. However you can create an intermediate class that contains your inner array and have an array of that class type. For example:

[System.Serializable]
public class GameObjectArray
{
    public GameObject[] items;
}

GameObjectArray[] tilesGrid;

So you have a 1d array of GameObjectArray objects and each of those object can have a nested 1d array of gameobjects.

However if you have already build your logic to work with a 2d array you can implement the ISerializationCallbackReceiver interface in your class and simply store your 2d array as a single flattened array. ps: multidimensional arrays can actually be slightly slower than a flattened 1d array, so it might be simpler to just use a flattened array.

Since you already store your width and height in your class you can simply replace

GameObject[,] tilesGrid;

with

GameObject[] tilesGrid;

And then replace all your uses of your tilesGrid from this:

tilesGrid[x, y]

with

tilesGrid[x + y * width]