Search Unity

Why do Prefabs lose array data when starting the game in editor?

Discussion in 'Editor & General Support' started by timothy-s-dev, Jun 3, 2022.

  1. timothy-s-dev

    timothy-s-dev

    Joined:
    Nov 7, 2012
    Posts:
    5
    I'm noticing some odd behavior around prefab-vs-unpacked objects with array properties. I have a MonoBehavior script with an array of a Serializable class as a public property. As expected, this shows up in the inspector, I can add and remove elements, and they persist when saving and quitting Unity, run the game from the editor, etc.

    What's odd is that if I put that MonoBehavior script on a prefab, and pull the prefab into the scene, suddenly I lose the contents of the array any time I run the game in the editor, or save and quit. If I unpack that prefab, then setup my array on the unpacked object, it works as expected again.

    Is this the intended/expected behavior? Non-array properties on the same script seem to keep their values. Feel like I must be missing something here. I'm guessing it's pulling the empty list from the prefab for some reason, but I'm not sure why it's only doing that for the array.
     
  2. timothy-s-dev

    timothy-s-dev

    Joined:
    Nov 7, 2012
    Posts:
    5
    Discovered that the issue only happened when the values were being set by an editor script, and from there was able to find the `EditorUtility.SetDirty()` method which, when called on every object that gets updated in the script, fixed the issue of the values being lost.