*Deliberately* resetting a ScriptableObject?

Hi guys, Google is polluted from topics on ScriptableObject ‘resetting on play’ (because you should set it dirty) - so I have a hard time finding good info on this:

I want to ‘reset’ a ScriptableObject to it’s default values - is that possible?

–background–

I am doing an Editor script and offer the user consistency through Play/stop/close/open Unity - by saving & loading a range of variables in a ScriptableObject in the background.

All is fine, but if my user would like to ‘reset’ the application, it’d be rather clean code if I could just ‘reset to default values’ - is that possible somehow? (I’m not including the ‘instancer’ in my build, but I can do a lot of hacks, but I just wonder if there’s a more clean way to simply ‘reset the object to default values’?

Thanks :slight_smile:

Hey, this seems to do the trick:

You may call it when exiting play mode.

Cheers!

I don’t understand why these objects don’t revert like everything else in Unity. Here’s a workaround to handle the unloading automatically:

using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif

public class Helpers
{
    static private HashSet<UnityEngine.Object> objectsToUnload = new HashSet<UnityEngine.Object>();
    static private void UnloadResources()
    {
        if(Application.isPlaying)
            return;

        foreach(UnityEngine.Object obj in objectsToUnload)
            Resources.UnloadAsset(obj);
        objectsToUnload.Clear();
    }

    static public T LoadResourceWithAutoUnload<T>(string name) where T: UnityEngine.Object
    {
        T result = Resources.Load<T>(name);

#if UNITY_EDITOR
        if(result is ScriptableObject)
        {
            objectsToUnload.Add(result);
            EditorApplication.playmodeStateChanged += UnloadResources;
        }
#endif

        return result;
    }
}

@Brankov
No, sorry, made the below - which appears to me to be an evil hack, but sort of does the job. One could also overwrite values one by one, but I like a ‘Reset’ to be as much of a ‘Reset’ as possible.

  public static void resetToDefault()
  {
    AssetDatabase.DeleteAsset(refToSettingsFilePath);
    CreateAsset<SettingsFile>();
    Debug.Log("System X reset while running. This is fine, but will likely cause alerts from Unity above or below this line, they can just be ignored.");
  }

You could put your data inside a serializable struct (inside the ScriptableObject) and when you want to trigger reset, reassign the struct with default construction.

[Serializable] struct Config {...}
// ...
m_Config = new Config();

Here’s another approach: Scriptable Objects: How to keep original values. | by Gaspar Errobidart | Medium

I don;t think scriptable objects have a default value, I would just make a copy of it and name it default and then make your scriptable object = the default values, or put default values in the scriptableObject.