0
0
Unityframework~10 mins

DontDestroyOnLoad usage in Unity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - DontDestroyOnLoad usage
Start Scene
Create GameObject
Call DontDestroyOnLoad
Load New Scene
GameObject persists
Continue Game with same GameObject
This flow shows how a GameObject is created, marked to not be destroyed, and then persists across scene loads.
Execution Sample
Unity
void Awake() {
  DontDestroyOnLoad(gameObject);
}

// Load new scene later
This code marks the GameObject to persist when a new scene loads.
Execution Table
StepActionGameObject StateScene LoadedResult
1GameObject created in Scene 1Exists in Scene 1Scene 1GameObject active
2DontDestroyOnLoad called on GameObjectMarked to persistScene 1GameObject will not be destroyed on scene load
3Load Scene 2Still existsScene 2GameObject persists across scenes
4Game continues in Scene 2Exists in Scene 2Scene 2GameObject usable in new scene
5Load Scene 3Still existsScene 3GameObject persists again
6EndGameObject still aliveScene 3Execution stops
💡 Execution stops after GameObject persists through scene loads
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 5Final
gameObjectCreated in Scene 1Marked DontDestroyOnLoadExists in Scene 2Exists in Scene 3Still exists
Key Moments - 3 Insights
Why doesn't the GameObject get destroyed when a new scene loads?
Because DontDestroyOnLoad was called on the GameObject (see Step 2 in execution_table), it is marked to persist and Unity keeps it alive across scene loads.
Does DontDestroyOnLoad prevent the GameObject from being destroyed manually?
No, DontDestroyOnLoad only prevents destruction during scene loads. The GameObject can still be destroyed by code or user actions.
What happens if you call DontDestroyOnLoad on multiple GameObjects?
Each marked GameObject will persist across scenes independently, allowing you to keep multiple objects alive (not shown in this trace but same principle applies).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step is DontDestroyOnLoad called on the GameObject?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Check the 'Action' column in execution_table for when DontDestroyOnLoad is called
According to variable_tracker, what is the state of the GameObject after loading Scene 3?
AExists in Scene 3
BMarked DontDestroyOnLoad but destroyed
CDestroyed
DNot created yet
💡 Hint
Look at the 'After Step 5' column for gameObject in variable_tracker
If DontDestroyOnLoad was not called, what would happen at Step 3 when loading Scene 2?
AGameObject would persist
BScene would not load
CGameObject would be destroyed
DGameObject would duplicate
💡 Hint
Recall that without DontDestroyOnLoad, GameObjects in the old scene are destroyed on scene load
Concept Snapshot
DontDestroyOnLoad usage in Unity:
- Call DontDestroyOnLoad(gameObject) in Awake or Start
- Marks GameObject to persist across scene loads
- Prevents destruction only during scene changes
- Useful for managers, audio, or player objects
- GameObject remains active and usable in new scenes
Full Transcript
This visual execution trace shows how DontDestroyOnLoad works in Unity. First, a GameObject is created in the starting scene. Then, DontDestroyOnLoad is called on it, marking it to persist. When a new scene loads, normally all objects are destroyed, but this GameObject stays alive. It continues to exist and be usable in the new scene. The variable tracker confirms the GameObject's state after each step. Key points include that DontDestroyOnLoad only prevents destruction during scene loads, not manual destruction. The quiz questions help reinforce understanding of when and why the GameObject persists.