Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

EditorUtility.SetDirty Broken?

Discussion in 'Editor & General Support' started by SteveJ, Dec 21, 2015.

  1. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,066
    Has anyone been experiencing any issues with EditorUtility.SetDirty() in Unity 5.3.x? I've just noticed that it no longer seems to be working for me - I use it with some of my level builder code but haven't built any new levels in a few weeks. It's possible that it stopped in 5.3.0 and I didn't notice it.

    Just want to confirm if anyone else is having issues with it?
     
  2. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    639
    hi

    By not working do you mean that it no longer marks the scene dirty and thus your scene is not saved when do ctrl-s?
     
  3. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,614
  4. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,066
    Pretty much this. So I make a change to an object through an Editor script and mark it as dirty. Then I save the scene, load a different scene, come back to the original scene - and the changes I made weren't saved.
     
  5. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,614
    Yep, follow the link in my post to the Upgrade Guide and read the 'Editor Extensions' section near the bottom.
     
  6. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,066
    Sweet! Thanks :)
     
  7. RichCodes

    RichCodes

    Joined:
    Apr 3, 2013
    Posts:
    142
    So following the Upgrade Guide,

    Undo.RecordObject(targetObject, "Edit label");
    targetObject.ChangedThing = newValue;
    EditorUtility.SetDirty(targetObject);


    *Edit* I did have a problem that I was asking about, but of course I had an epiphany moments after assuming it was Unity's fault. (I noticed the asterisk showing that I had failed to save my own code file.) I left my working pseudo-code up, hoping it helps others.

    I'm sorry Unity, for assuming it was your fault the SetDirty wasn't working correctly!
     
    Last edited: Jan 4, 2016
  8. joaobsneto

    joaobsneto

    Joined:
    Dec 10, 2009
    Posts:
    152
    Hi,

    It's not working on Unity 2017.3.0f3 when I try to edit a ScriptableObject of Playmaker FSM Template. I'm using:

    Code (CSharp):
    1.  
    2. Undo.RecordObject(template, "Update FSM time in file " + template.name);
    3. ConversationTimeExtractor.ProcessTimeInFsm(ref template, inputData, utils);
    4. EditorUtility.SetDirty(template);
    5.  
    My guess is that the editor is not detecting that the asset has changed, because it does not generate the Undo option. When I use "Undo.RegisterFullObjectHierarchyUndo", the undo option is registered, but when I hit play, the object reset to previous state.
     
  9. DerDicke

    DerDicke

    Joined:
    Jun 30, 2015
    Posts:
    291
    Had the same problem and could solve it. It was a Serialization issue. Maybe this can help some people.

    I have a ScriptableObj 'SoundTable', holding an array of 'SoundClip's, each associating an AudioClip with a string key.
    For faster access I collect double entries and save them with the SoundTable. So I had a SoundClip[][] in the table. That worked mighty fine in the Editor, but didn't survive Play Mode and didn't save to disk.

    Because it worked in Editor, i thought it was an issue with dirty flag and used SetDirty(). Didn't work. The solution was to avoid the mulidimensional array by putting the array in an class, like this:

    Code (CSharp):
    1.    
    2. [System.Serializable]
    3.     public class SoundClipArray
    4.     {
    5.         public string name;
    6.         public SoundClip[] clips;
    7.         public SoundClipArray(string theName, SoundClip[] theArray)
    8.         {
    9.             name = theName;
    10.             clips = theArray;
    11.         }
    12.     }
    13.  
    SetDirty() now works as expected (for this use case). So the point is, serialization works different in Editor and when writing to disk. Evil trap I was falling into.