Array does not serialize properly in a prefab instance for a scene
I'm currently experiencing an issue where I attempt to link up some data in the scene for one of the prefab instances via an editor menu item script, and Unity (5.4.1f1 Personal Edition) will do one of 3 things:
It will serialize the array properly (size and all of its members). This rarely happens.
It will partially serialize the array (size will be correct, and some of its elements will be as well). This often happens.
It will minimally serialize the array (size will be correct, but NONE of the elements will be). This sometimes happens.
I ensure that I call Undo.RecordObject, and I've tried calling it directly on the game object, then the Enemy script, and then both with no improvements. I also make sure to call EditorSceneManager.MarkSceneDirty as well. I know when something has gone wrong because the text in the inspector will not be bolded, and when the scene is re-loaded, all of the non-bolded array elements will be null.
I've already spent far too long trying to figure out a pattern to this behaviour but there doesn't seem to be one and I'm hoping there's something I'm missing that will fix this issue.
If I reloaded the scene, elements 0 (inclusive) to 4 (inclusive) will be null, and thus, my game logic breaks.
Edit 1: I've found a temporary work around. What I have to do is click on an element of the array (the circle with a dot in the center), and then select a different item. At that point, all of the items in the array become properly bolded and they also serialize properly. And of course, I then change the item back to the original intended data. This method is a little unusual but it works.
All that should be required is a) Undo.RecordObject(), specifying the Enemy behaviour as the target object(s) and b) that all of the values set are persistent in the scene data. Could you possibly share any code where you are calling Undo.RecordObject()?
Sure, here's the gist of it:
Undo.RecordObject(SelectedEnemy, "Setting Waypoints (Enemy)");
SelectedEnemy.PatrolPoints = WalkableTiles.ToArray();
EditorScene$$anonymous$$anager.$$anonymous$$arkSceneDirty(Scene$$anonymous$$anager.GetActiveScene());
SelectedEnemy is the Enemy script that you see in the screenshot. PatrolPoints is a public array of LevelTiles, all of which exist in the scene (they're also linked to a prefab). I don't know how helpful this is so if you want the entire function for this, let me know and I'll post it.
I am assu$$anonymous$$g you have verified that WalkableTiles.ToArray() contains all the correct items when you set the value?
Yeah, it does contain all of the correct items. Each Hexagon is a game object in the scene with a LevelTile script attached to it, and the Enemy that's being modified is also in that same scene as well so I'm not sure why this is happening. I might have to experiment with other methods to work around this. :(
Can you give a screen shot for the prefab's inspector. Also where are these Hexagon(xxx) located?
Sure, here is the default prefab:
I've actually found a temporary work around, which I'll edit into the original post. The hexagons are located in the scene (they're also each an instance of a prefab).
Well, you could look into the scene file and find out what's going on there. Unity's scene file will list out the prefab changes separately. If you can reproduce this issue, plz send a bug report to Unity.