Search Unity

ScriptableObject asset problem! The changes won't saved to disk!

Discussion in 'Editor & General Support' started by 5argon, Feb 20, 2014.

  1. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,555
    I create an instance of ScriptableObject onto my disk as an .asset file.

    Then I planned to use that file at runtime so I create a public variable in one of my component as that ScriptableObject type so the slot would show up in the inspector. I then drag my .asset file to that slot and I can use its value as soon as I enter play mode and I see changes immediately applied to my .asset file by clicking on the file in my asset tree and I see the Inspector showing the changed value that my script changes.

    However, the actual data on disk haven't change yet! I tried opening that file on my disk (I use 'Force Text' serialization so I can read it) and the value in that file is not like in the inspector! I can enter play mode and exit as many time as I wanted and the value is saved across plays but never to my file in disk. The real problem starts when I closed unity and open again, the value has gone because it was never saved to disk.

    If I push Ctrl+D to duplicate the file, the Inspector of new file will show the value on disk of original file, not the updated ones in the inspector. Supporting the evidence that the value on disk is not getting updated.

    BUT one way that I got it to finally update is, I changed one of the field that show up in .asset inspector by hand in editor mode. Then the moment I close Unity the file will be updated! I wondered why when I used the script to change values (adding Note objects to the list, as seen below) the file refuse to update when I close unity and simply discarding changes? (Of course the ideal solution is I want it to save immediately, not when closing unity.)

    Can someone help me so I can update changes to disk?

    Here is my class hierarchy. I got it to save when closing Unity by, for example, changing the 'bpm' number to something else in the .asset file inspector and close Unity.

    Code (csharp):
    1. public class Simfile : ScriptableObject {
    2.  
    3.     public string songName;
    4.     public string artist;
    5.     public string noteCharter;
    6.     public AudioClip musicFile;
    7.     public int bpm;
    8.     public float offset;
    9.    
    10.     [SerializeField] Chart easyChart;
    11.     [SerializeField] Chart normalChart;
    12.     [SerializeField] Chart expertChart;
    13. .
    14. .
    15. .
    16.  
    17. [System.Serializable]
    18. public class Chart
    19. {
    20.  
    21.     Note focusedNote = null;
    22.  
    23.     [SerializeField] int laneCount;
    24.     [SerializeField] int noteCount;
    25.     [SerializeField] public List<Note> notes;
    26. .
    27. .
    28. .
    29.  
    30. [System.Serializable]
    31. public class Note {
    32.     [SerializeField] float position;
    33.     [SerializeField] float absolutePosition;
    34.     [SerializeField] int specialNote;
    35.     [SerializeField] int lane;
    36.     [SerializeField] float nextLinkAbsolutePosition;
    37.     [SerializeField] int nextLinkLane;
    38.     [SerializeField] float prevLinkAbsolutePosition;
    39.     [SerializeField] int prevLinkLane;
    40. .
    41. .
    42. .
    43.  
    Thank you
     
    Last edited: Feb 20, 2014
  2. albal

    albal

    Joined:
    Dec 4, 2012
    Posts:
    1
    If you want to save that asset you have to call
    Code (csharp):
    1. AssetDatabase.Refresh()
    ; first, then you have to mark the object that you load from the .asset file as dirty
    Code (csharp):
    1. EditorUtility.SetDirty(object loaded from .asset file);
    and then call
    Code (csharp):
    1. AssetDatabase.SaveAssets();
    :)
     
  3. games4ever

    games4ever

    Joined:
    Jan 23, 2014
    Posts:
    11
    Great answer @albal, this worked for me.
     
  4. AndreiMarian

    AndreiMarian

    Joined:
    Jun 9, 2015
    Posts:
    77
    I'm struggling with this issue for days and still can't figure out how to do it.
    I tried putting the code in _Awake()_ for each SO. This has a nasty effect: if there are 10 SO's it asks to save 100 times!! Oh, and upon restarting Unity the assets are empty, not of ScriptableObject type - as if no code was written at all.
    So where to put this code?
     
  5. sunwangshu

    sunwangshu

    Joined:
    Mar 2, 2017
    Posts:
    21
    Very helpful. Thanks!!!!
     
  6. Ali_V_Quest

    Ali_V_Quest

    Joined:
    Aug 2, 2015
    Posts:
    138
    I think just setting it dirty is enough ( worked for me )
     
    DSivtsov and JoeJoe like this.
  7. MrLucid72

    MrLucid72

    Joined:
    Jan 12, 2016
    Posts:
    992
    Wait, WHERE do you save this? How do you detect changes? Is there like an OnValueChanged() or something?
     
  8. JoeJoe

    JoeJoe

    Joined:
    Mar 22, 2013
    Posts:
    8
    Oh this baffled me for days. All I needed was SetDirty.
    MrLucid, you would do this in the same script that is updating your SO's. If you're changing an SO field value "by hand" in the editor, it's not an issue, your changes will persist, unless you're in play mode, in which case... well stop playing and then change the value.
     
  9. fct509

    fct509

    Joined:
    Aug 15, 2018
    Posts:
    108
    SetDirty is not longer an option, and I can't try this fix.
     
  10. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    How is SetDirty no longer an option?
     
  11. fct509

    fct509

    Joined:
    Aug 15, 2018
    Posts:
    108
    I'm on Unity 2019.4, and SetDirty() has been deprecated from the ScriptableObject API. We might be able to use EditorUtility.SetDirty(someObject) instead.
     
    DSivtsov likes this.
  12. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    Ah, I thought you were talking about
    EditorUtility.SetDirty
    . I had forgotten about the existence of the version on ScriptableObject. Yes, you want to use
    EditorUtility.SetDirty
    in these instances.
     
    DSivtsov likes this.
  13. marufdhaka

    marufdhaka

    Joined:
    Oct 25, 2020
    Posts:
    4
    IT Only work for unity editor not in mobile device