What if your game's music and score could magically survive every scene change without extra code?
Why DontDestroyOnLoad usage in Unity? - Purpose & Use Cases
Imagine you are making a game where the player's score or music should keep playing even when moving between different game levels or scenes.
Without special handling, every time you load a new scene, all objects from the previous scene disappear, and you lose the score or music.
Manually copying data or restarting music every time a new scene loads is slow and error-prone.
You might forget to save or restore something, causing bugs or a bad player experience.
DontDestroyOnLoad lets you mark important game objects so they stay alive and keep working across scene changes automatically.
This means your music keeps playing and your score stays intact without extra work.
SceneManager.LoadScene("Level2"); // music stops, score resetsDontDestroyOnLoad(gameObject); // music and score persist across scenesYou can create smooth, continuous gameplay experiences where key objects survive scene changes effortlessly.
In a game, the background music player object uses DontDestroyOnLoad so the music never stops even when the player moves from the main menu to the game level.
Scene changes normally destroy all objects, losing data or effects.
DontDestroyOnLoad keeps marked objects alive across scenes.
This makes managing persistent data or effects easy and reliable.
Practice
DontDestroyOnLoad in Unity?Solution
Step 1: Understand what
This method prevents a GameObject from being destroyed when a new scene loads.DontDestroyOnLoaddoesStep 2: Identify the purpose in scene management
It keeps important objects like music players or managers alive across scenes.Final Answer:
To keep a GameObject alive when switching scenes -> Option DQuick Check:
DontDestroyOnLoad keeps objects alive = A [OK]
- Thinking it destroys objects instead of preserving them
- Confusing it with scene loading functions
- Assuming it pauses the game
DontDestroyOnLoad in a Unity script?Solution
Step 1: Identify the correct parameter for DontDestroyOnLoad
The method requires a GameObject to keep alive, usually referenced asgameObjectin scripts.Step 2: Check each option
DontDestroyOnLoad(gameObject); usesgameObject, which is correct. DontDestroyOnLoad(this); usesthis(the script), which is incorrect. Options B and C use unrelated types.Final Answer:
DontDestroyOnLoad(gameObject); -> Option CQuick Check:
Use gameObject with DontDestroyOnLoad = D [OK]
- Passing 'this' instead of 'gameObject'
- Trying to pass Scene or SceneManager objects
- Calling DontDestroyOnLoad without parameters
void Awake() {
DontDestroyOnLoad(gameObject);
}
void Start() {
Debug.Log("Object alive");
}
What will happen when the scene changes?Solution
Step 1: Understand when Awake and Start run
Awake runs once when the object is created. Start runs once after Awake. Both run only once per object lifetime.Step 2: Effect of DontDestroyOnLoad on scene change
The GameObject is kept alive across scenes, so Awake and Start do not run again on scene load.Final Answer:
The GameObject will persist and "Object alive" will print once -> Option AQuick Check:
DontDestroyOnLoad keeps object alive, Awake/Start run once = A [OK]
- Thinking Start runs on every scene load
- Assuming object duplicates on scene change
- Believing object is destroyed on scene change
void Start() {
DontDestroyOnLoad(this);
}
What is the problem and how to fix it?Solution
Step 1: Identify the parameter type for DontDestroyOnLoad
DontDestroyOnLoad requires a GameObject, but 'this' refers to the script instance, not the GameObject.Step 2: Correct the parameter
UsegameObjectto pass the GameObject the script is attached to.Final Answer:
Error: 'this' is not a GameObject; fix by using DontDestroyOnLoad(gameObject); -> Option BQuick Check:
DontDestroyOnLoad needs gameObject, not this = C [OK]
- Passing 'this' instead of 'gameObject'
- Thinking DontDestroyOnLoad must be in Awake only
- Passing wrong types like Scene or SceneManager
DontDestroyOnLoad?Solution
Step 1: Understand the problem of duplicates
Using DontDestroyOnLoad alone keeps the object alive, but reloading the scene creates duplicates.Step 2: Use a check to destroy duplicates
void Awake() { if (FindObjectsOfType<MusicPlayer>().Length > 1) { Destroy(gameObject); } else { DontDestroyOnLoad(gameObject); } } checks if more than one MusicPlayer exists and destroys the extra one, preventing duplicates.Step 3: Verify other options
void Awake() { DontDestroyOnLoad(gameObject); } keeps duplicates. void Start() { Destroy(gameObject); DontDestroyOnLoad(gameObject); } destroys then calls DontDestroyOnLoad (wrong order). void Awake() { Destroy(gameObject); } destroys always.Final Answer:
void Awake() { if (FindObjectsOfType<MusicPlayer>().Length > 1) { Destroy(gameObject); } else { DontDestroyOnLoad(gameObject); } } -> Option AQuick Check:
Check duplicates before DontDestroyOnLoad to avoid duplicates = B [OK]
- Not checking for duplicates causing multiple music players
- Destroying object before calling DontDestroyOnLoad
- Calling DontDestroyOnLoad without duplicate check
Ready to prove it?
Pass the test and this pattern moves unknown
