Force serializable field to save on prefab

I have a problem with prefabs with serialized fields. Here’s the scenario:

  • I have a script which sets a value in a private field marked as serializable. Setting the value manually is not feasible, as I will have potentially hundreds of these in a scene, but I need the value to be serialised.
  • The script is set to [ExecuteInEditor] and, on Awake(), successfully sets the correct value in the field if it is not previously set from a serialized value.
  • This works correctly for all objects except prefabs. Prefabs work if and only if I select each one and manually touch the value in the Inspector after it has been set. If I do not, the value is displayed as if it were the default value and does not get serialized, causing errors the next time the scene is loaded.

Is there any way I can force the prefab to recognise that this value needs to be saved?

1 Like

Solved: Unity - Scripting API: EditorUtility.SetDirty

1 Like

Cannot imagine how you made it work. This problem is one of the mind-boggling mysteries of Unity.

I found it also easy to solve via EditorUtility.SetDirty.

1 Like