Search Unity

Updating prefab variable via script doesn't save override

Discussion in 'Prefabs' started by hololabs, Jan 8, 2019.

  1. hololabs

    hololabs

    Joined:
    Nov 15, 2012
    Posts:
    20
    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.

    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.

    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
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    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.
     
    Kokowolo likes this.
  3. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
  4. hololabs

    hololabs

    Joined:
    Nov 15, 2012
    Posts:
    20
    Thank you! PrefabUtility.RecordPrefabInstancePropertyModifications was exactly what I was looking for.
     
  5. launzone

    launzone

    Joined:
    Dec 2, 2015
    Posts:
    57
  6. Kokowolo

    Kokowolo

    Joined:
    Mar 26, 2020
    Posts:
    60