Updating prefab variable via script doesn't save override

Hi,

I’ve come across an issue with prefab overrides and I’m wondering if there is a way to set a prefab to dirty in order to save overrides via script.

I have a prefab that gets an id value changed via script. This id change isn’t getting saved in scene nor being flagged as an override on the prefab.

4077934--355600--unity-prefab-override-1.PNG

If I manually update the value on the prefab it registers as an override and I can save it within the scene.

4077934--355603--unity-prefab-override-2.PNG

Is there a way to set this object as dirty in a script? or can I touch some other property that will flag it as dirty.

Thanks,

Amesh

How are you setting it dirty?

If you’re using EditorUtility.SetDirty, that’s been broken by design for ages, and you have to also manually set the scene dirty with the scene manager. Alternatively, use SerializedObject/SerializedProperty with ApplyModifiedPropertiesWithoutUndo.

1 Like

Check out Unity - Scripting API: PrefabUtility.RecordPrefabInstancePropertyModifications

1 Like

Thank you! PrefabUtility.RecordPrefabInstancePropertyModifications was exactly what I was looking for.

Having trouble with this, I delete a referenced object through the editor and set it to null, after that I try to do everything from RecordPrefabInstanceProperty, SerializeObject and ApplyModifiedProperties to setting it to IsPersistent. Still my modifications to the prefab won’t get saved.

To summarize:
If you are changing the serialized values by script…
Calling UnityEditor.PrefabUtility.RecordPrefabInstancePropertyModifications(YOUR_COMPONENT) on your instance’s component will solve this problem. Note that if you want the Editor to detect the scene change you also have to call UnityEditor.EditorUtility.SetDirty(YOUR_INSTANCE) as well.