Hi, I am having some trouble understanding how saving using Undo.* works. I currently have scriptable objects that I am using to pass data between scenes. I am setting some options in the main menu, and I want to use those options in the game. This is my current code to save the object every time it is modified.
[CreateAssetMenu]
public class IntVariable : ScriptableObject
{
[SerializeField] private int _variable;
public int Variable
{
get => _variable;
set
{
_variable = value;
// EditorUtility.SetDirty(this);
}
}
}
If I uncomment the SetDirty line, then it all works exactly how I want it to; however, looking online (Unity - Scripting API: EditorUtility.SetDirty), SetDirty apparently shouldn’t be used anymore, I assumed it was becoming deprecated. Instead of SetDirty, Unity says we are supposed to use Undo.RecordObject(). I tried doing that with this code instead
public class IntVariable : ScriptableObject
{
[SerializeField] private int _variable;
public int Variable
{
get => _variable;
set
{
Debug.Log(EditorUtility.IsDirty(this));
Undo.RecordObject(this, "Change int");
_variable = value;
// Undo.FlushUndoRecordObjects();
Debug.Log(EditorUtility.IsDirty(this));
}
}
}
With debugs to tell if it was actually dirty like it was supposed to be, and it both says it isn’t dirty, and when I close and reopen the project, nothing indeed has been saved. Is there something we need to call in order to save these default values, or am I just using the syntax wrong, or am I just going about this whole thing in the wrong way?