Save ScriptableObject In Editor Script

hello folks, I’m having trouble saving scriptable objects’ data through an editor script, this is what I’m using to save them atm

public override void OnInspectorGUI() { if (GUILayout.Button("Force Save")) { AssetDatabase.Refresh(); EditorUtility.SetDirty(target); AssetDatabase.SaveAssets(); } }

however, whenever I restart the editor all the data is lost. Does anyone know what I should do?

Afaik, you need to use the Undo class from the Editor namespace to record the last state of the object before changing the value. This is the way “set dirty” works in the Unity Editor.

public override void OnInspectorGUI() 
{
    if (GUILayout.Button("Set Value"))
    { 
        Undo.RecordObject(target, "Setting Value");
        (target as WhatEver).myValue = myNewValue;
    }
}