Managed Scriptable Object

To understand the usage of this class, it's important to understand Unity's Enter Play Mode Options and the effect that not reloading the domain can have on ScriptableObjects.

The lifetime of a ScriptableObject, unlike a GameObject in the scene, doesn't start and finish with Play Mode. An SO can call its OnEnable only once when the editor is started (or when code recompiles), so its state is not cleared when entering Play Mode (like it happens to GameObjects and their scripts).

This class, ManagedScriptableObject, exposes a method called Reset() which will be invoked when exiting Play Mode, offering you a chance to reset the SO's state.

For more information, read all about Unity's Enter Play Mode Options and especially disabling Domain Reload on their documentation.


Using this class

Inheriting from it

To use Managed Scriptable Object in your scripts, simply inherit from it and implement the Reset() method.

For instance, if you wanted to make an SO that holds the score of the game, you want to make sure to reset it when the editor exits Play Mode, like this:

[CreateAssetMenu]
public class MySmartSO : ManagedScriptableObject
{
    public int score;

    public void AddPoints(int points) => score += points;
    
    protected override void Reset()
    {
        score = 0;
    }
}

This way, even if no one sets the score to zero, upon exiting Play Mode the Reset method will be invoked.

Last updated