In this lesson, you will learn how to use ScriptableObjects to maintain your game's global state and settings as you transition between scenes without needing DontDestroyOnLoad objects.
Why Avoid DontDestroyOnLoad?
Often, beginners use a "Manager" GameObject with DontDestroyOnLoad to carry data (like player health or high scores) across scenes. While functional, this can lead to messy hierarchies and "zombie" objects that persist unintentionally. Because ScriptableObjects exist as independent assets in your Project folder rather than items in your scene hierarchy, they are inherently persistent. When one scene finishes and another loads, the ScriptableObject stays in memory, acting as a reliable "single source of truth."
Implementing Global Data
To use this, create a class that inherits from ScriptableObject and defines the variables you want to track.
using UnityEngine;
[CreateAssetMenu(fileName = "GameData", menuName = "Data/GameData")]
public class GameData : ScriptableObject
{
public int playerHealth = 100;
public string currentLevelName;
}
After you create this asset in your Project folder, drag a reference to it into any script that needs the data.
For example, if a PlayerHealth script in Scene A reduces the playerHealth value, that change is saved directly into the asset file. When Scene B loads, the scripts attached to objects there can reference that same GameData asset and read the updated value immediately. Because the asset lives outside the scene hierarchy, it doesn't get destroyed when the scene changes; it is simply referenced by whichever objects need it at the time.
Why This Is Clean
This approach keeps your scene hierarchies tidy. You no longer need to worry about checking if a manager already exists or if you have multiple copies of a persistent object. You simply treat your ScriptableObject as a data container that survives as long as the game is running, allowing different scenes to "plug in" to the same data set effortlessly.
Key Takeaway: By using ScriptableObjects as centralized data containers, you can maintain persistent game state across scene transitions without cluttering your scene hierarchies with temporary manager objects.