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

[SOLVED] ScriptableObject is NOT getting saved!

Discussion in 'Editor & General Support' started by Deleted User, Apr 14, 2016.

  1. Deleted User

    Deleted User

    Guest

    I have a custom editor for my scriptable object and this is what I do:

    Code (CSharp):
    1.         public override void OnInspectorGUI()
    2.         {
    3.             var myTarget= (MyScript) target;
    4.             DrawEditor();
    5.             if (GUILayout.Button("Save"))
    6.             {          
    7.                 EditorUtility.SetDirty(myTarget);
    8.                 AssetDatabase.SaveAssets();
    9.             }
    10.         }
    When I click the save button, the editor saves everything (the mouse icon changes) but as soon as I navigate away from my scriptable object, it's fields are reset to default values! What gives?
     
    guneyozsan likes this.
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,795
  3. Deleted User

    Deleted User

    Guest

    Thank you! That solved it.
     
    karl_jones likes this.
  4. NeverTrustShadows

    NeverTrustShadows

    Joined:
    Mar 15, 2020
    Posts:
    13
    JBR-games likes this.
  5. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,026
    This thread is several years old and in the time since then that documentation entry has changed multiple times.

    Unity 5.2 had an example of how to use it:
    https://docs.unity3d.com/520/Documentation/ScriptReference/EditorUtility.SetDirty.html

    Unity 5.3 said it was going to be deprecated:
    https://docs.unity3d.com/530/Documentation/ScriptReference/EditorUtility.SetDirty.html

    Unity 5.4 removed that warning and added a method to be called prior to calling it:
    https://docs.unity3d.com/540/Documentation/ScriptReference/EditorUtility.SetDirty.html

    Unity 2018.4 removed the reference to the method:
    https://docs.unity3d.com/2018.4/Documentation/ScriptReference/EditorUtility.SetDirty.html

    Unity 2019.1 added the method reference back:
    https://docs.unity3d.com/2019.1/Documentation/ScriptReference/EditorUtility.SetDirty.html

    Unity 2019.2 removed it again:
    https://docs.unity3d.com/2019.2/Documentation/ScriptReference/EditorUtility.SetDirty.html

    Unity 2020.2 they finally removed the notice altogether:
    https://docs.unity3d.com/2020.2/Documentation/ScriptReference/EditorUtility.SetDirty.html

    All of the above makes me wish there was a proper history for every page like you see on a wiki.
     
    Last edited: Nov 17, 2022
    V5Studio and JBR-games like this.