Proper way to save ScriptableObjects changed during play mode

I was wondering what the “correct” way to save scriptable objects in Unity that are changed during Play mode. So far I have something that works:

// changes
PartyMember Lalala = PartyMemberDictionary.Find("Lalala");
Lalala.HP += 17;
print("Lalala's HP: " + Lalala.HP);
// then saves

Note: Lalala is a direct reference to a PartyMember scriptable object in my Assets/Resources folder.

I’m still a little shaky on this approach, however. I got this directly from reading this question:

But the commentator did not explain what each of these three lines of code mean.


Can someone explain these to me? I’m not sure I understand them fully. I assume “dirty” indicates that data changed; please correct me if I’m wrong.

In this link

it asserts that SaveAssets() saves all unchanged asset changes to disk, so why do I need the other Refresh and SetDirty Functions?

In this link

The OP says “as I found out after some trial and error AssetDatabase.SaveAssets doesn’t actually save the asset as expected.” … so confused at this point. That leaves me hesitant to continue until I understand what’s going on. Perhaps the Refresh and SetDirty cement the job?

Also, in this link

it mentioned that Undo.RecordObject is preferred. Based on your experiences, is this true? Is EditorUtility.SetDirty(); obsolete in this day and age already? I don’t want to use it if I have to change my code in the future.

At the end of the day I simply want something that works.


What you are doing is intended for in-editor assets management not game saving

From here: Unity - Manual: The Asset Database The AssetDatabase interface is only available in the editor and has no function in the built player

Yep, scriptable objects and these functions are intended for editor purposes only and will not work once you build your project! I believe that scriptable objects get compiled with the rest of the scripts so they cant be changed at runtime(or maybe they can…correct me if I am wrong). So we need an external file to save data which doesnt get compiled and can be changed. To achieve that you create a folder in your assets called Streaming Assets(doesnt get compiled) or put your file externally on users device (A dedicated place for saves is Application.persistendatapath! As for the save file creation itself most common approach is to create a json, xml or custom binary file. Here are 6 videos explaining all of it and more (except json but it is similar):Saving Data in Unity: Series Introduction - YouTube

while your not sure about this code it still worked for my purposes and i will not need to edit it after i build my project. I am using the newest version of unity and those three lines of code still work great. I just replaced the lalala with this.