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

ScriptableObject reverting to previous state even after scene save, project save.

Discussion in 'Scripting' started by lifeisabeach, Aug 29, 2022.

  1. lifeisabeach

    lifeisabeach

    Joined:
    Apr 26, 2020
    Posts:
    47
    I have a number of scriptableObjects where I'm setting many parameters to configure levels.

    Some of the scriptableObjects are randomly reverting to a previous state, even after multiple scene saves and project saves.

    It is driving me crazy because after many hours configuring one, I will open another only to find that it has reverted to a past state. It isn't affected by the changes made to the others, it really is reverting to a previous state while more recent changes are kept in others.

    I have lost a lot of work because of that. I'm at a point where I don't know if I should change my approach and not use scriptableObjects.

    I have created a class to keep saving the project, while using the editor or during play, and placed
    Code (CSharp):
    1. EditorApplication.ExecuteMenuItem("File/Save Project");
    there thinking it would help.

    I am thinking it might be because I have duplicated those scriptableObjects from an initial one, and grew from there. If that is the problem, is there a way to fix this for those I have created already? And how should I go about building a scriptableObject from a previous one?

    It is also weird because I test closing the scene, restarting play, a lot, and it seems like it is saved. But then, just now that I had Unity closed during the weekend, I open it and found some of them reverted to what I did at the beginning of Friday while others I edited late in that day are intact.

    I am using git, I don't know if that matters.

    Thank you
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    Are you calling
    EditorUtility.SetDirty()
    on the ScriptableObject instance itself??

    Code (csharp):
    1.         EditorUtility.SetDirty(filthyDirtyChangedAsset);
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    This matters a LOT and is GREAT because you can instantly see what got changed.

    BUT!!!!! Huge warning: Unity does not write changes to disk until you press either Save-Scene or Save-Project. (Or also if you leave Unity editor entirely, obviously, as long as you dirtied above)

    Is that perhaps part of your problem too?
     
  4. lifeisabeach

    lifeisabeach

    Joined:
    Apr 26, 2020
    Posts:
    47
    Thank you for the response @Kurt-Dekker !

    I was not calling
    EditorUtility.SetDirty()
    . I had read about it but was confused about its use.

    I have given it a try and it seems like it is working. Undos are not a major concern, so I think this will solve the issue just fine.

    Thanks again!
     
  5. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,524
    That's usually not necessary unless you change the values of your scriptable object by script or you use a faulty custom editor. The default editor should handle undos and serialization just fine. The issues you describe would be quite typical if the changes hasn't been properly communicated to Unity.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    Flush twice and set dirty... it's a long way to the Asset Database.