Persisting changes#

If you're modifying something from an editor context it needs to be dirtied.
If something is changed and not dirtied, that change is transient and will be wiped.

Options in order of preference:#

1. SerializedObject and SerializedProperty#

Although daunting at first, SerializedObject is a powerful system to record changes and automatically display extendable UI through property drawers. To learn how to access serialized fields and sub-objects, see SerializedObject: how-to.

Call ApplyModifiedProperties after making property modifications to apply persistent changes.

This will also record an undo; if you don't want an undo, use ApplyModifiedPropertiesWithoutUndo.
If your Object can be modified externally, you should call Update before drawing the fields to synchronise the SerializedObject with the base object.

2. Undo.RecordObject#

Call RecordObject before directly making changes to an Object to apply persistent changes1.

To correctly handle prefab changes, call RecordPrefabInstancePropertyModifications after RecordObject.

3. EditorUtility.SetDirty#

The final option for recording modifications is SetDirty. This will not record undo states.

If you're still not seeing changes persist, ensure that the variables are actually serializable.

  1. The diff is run at the end of the frame. So if you're using this in combination with SaveOpenScenes in one frame, the scene will not be marked as dirty, and it will not be saved. If you have this issue, you can use delayCall to save the Scene a little later, or manually mark the scenes as dirty.