📖 Scenario: You are making a simple Unity game where you want a music player object to keep playing music even when you change scenes. Normally, when you load a new scene, all objects are destroyed and recreated. But you want the music to continue without restarting.
🎯 Goal: Build a Unity script that uses DontDestroyOnLoad to keep a GameObject alive across scene changes.
📋 What You'll Learn
Create a C# script called MusicPlayer
In MusicPlayer, use DontDestroyOnLoad(gameObject) to keep the object alive
Add a check to prevent duplicates if the object already exists
Print a message to the console when the object is kept alive
💡 Why This Matters
🌍 Real World
Games often need objects like music players or managers to persist across scenes without restarting or duplicating.
💼 Career
Understanding how to manage persistent objects is important for game developers working with Unity to create smooth player experiences.
Progress0 / 4 steps
1
Create the MusicPlayer script with Awake method
Create a C# script called MusicPlayer with an Awake() method that prints "MusicPlayer Awake" to the console.
Unity
Hint
The Awake() method runs when the object is created. Use Debug.Log to print messages.
2
Add DontDestroyOnLoad to keep the object alive
Inside the Awake() method, add DontDestroyOnLoad(gameObject); to keep the GameObject alive when loading new scenes.
Unity
Hint
Use DontDestroyOnLoad(gameObject); inside Awake() to keep the object alive across scenes.
3
Prevent duplicate MusicPlayer objects
Modify the Awake() method to check if another MusicPlayer already exists. If yes, destroy this duplicate object using Destroy(gameObject);. Use FindObjectsOfType<MusicPlayer>() to find existing objects.
Unity
Hint
Use FindObjectsOfType<MusicPlayer>() to find all MusicPlayer objects. If more than one exists, destroy the new one.
4
Print confirmation when object is kept alive
Add a Debug.Log message inside Awake() after DontDestroyOnLoad(gameObject); that prints "MusicPlayer will not be destroyed on load".
Unity
Hint
Print a message after calling DontDestroyOnLoad to confirm the object stays alive.
Practice
(1/5)
1. What is the main purpose of using DontDestroyOnLoad in Unity?
easy
A. To pause the game during scene loading
B. To destroy a GameObject immediately
C. To load a new scene asynchronously
D. To keep a GameObject alive when switching scenes
Solution
Step 1: Understand what DontDestroyOnLoad does
This method prevents a GameObject from being destroyed when a new scene loads.
Step 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 D
Quick Check:
DontDestroyOnLoad keeps objects alive = A [OK]
Hint: Remember: DontDestroyOnLoad means object stays between scenes [OK]
Common Mistakes:
Thinking it destroys objects instead of preserving them
Confusing it with scene loading functions
Assuming it pauses the game
2. Which of the following is the correct way to use DontDestroyOnLoad in a Unity script?
easy
A. DontDestroyOnLoad(this);
B. DontDestroyOnLoad(Scene);
C. DontDestroyOnLoad(gameObject);
D. DontDestroyOnLoad(SceneManager);
Solution
Step 1: Identify the correct parameter for DontDestroyOnLoad
The method requires a GameObject to keep alive, usually referenced as gameObject in scripts.
Step 2: Check each option
DontDestroyOnLoad(gameObject); uses gameObject, which is correct. DontDestroyOnLoad(this); uses this (the script), which is incorrect. Options B and C use unrelated types.
Final Answer:
DontDestroyOnLoad(gameObject); -> Option C
Quick Check:
Use gameObject with DontDestroyOnLoad = D [OK]
Hint: Use gameObject, not this or Scene, with DontDestroyOnLoad [OK]
Common Mistakes:
Passing 'this' instead of 'gameObject'
Trying to pass Scene or SceneManager objects
Calling DontDestroyOnLoad without parameters
3. Consider this Unity C# script attached to a GameObject:
A. The GameObject will persist and "Object alive" will print once
B. The GameObject will be destroyed and "Object alive" will not print again
C. The GameObject will persist and "Object alive" will print every scene load
D. The GameObject will be duplicated on each scene load
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 A
Quick Check:
DontDestroyOnLoad keeps object alive, Awake/Start run once = A [OK]
Hint: Awake/Start run once; object stays alive with DontDestroyOnLoad [OK]
Common Mistakes:
Thinking Start runs on every scene load
Assuming object duplicates on scene change
Believing object is destroyed on scene change
4. You wrote this code in a Unity script:
void Start() {
DontDestroyOnLoad(this);
}
What is the problem and how to fix it?
medium
A. Error: DontDestroyOnLoad must be called in Awake, not Start
B. Error: 'this' is not a GameObject; fix by using DontDestroyOnLoad(gameObject);
C. No problem; code works fine as is
D. Error: DontDestroyOnLoad requires a Scene parameter
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
Use gameObject to pass the GameObject the script is attached to.
Final Answer:
Error: 'this' is not a GameObject; fix by using DontDestroyOnLoad(gameObject); -> Option B
Quick Check:
DontDestroyOnLoad needs gameObject, not this = C [OK]
Hint: Pass gameObject, not this, to DontDestroyOnLoad [OK]
Common Mistakes:
Passing 'this' instead of 'gameObject'
Thinking DontDestroyOnLoad must be in Awake only
Passing wrong types like Scene or SceneManager
5. You want to keep a music player GameObject alive across scenes but avoid duplicates if the scene reloads. Which code snippet correctly implements this using DontDestroyOnLoad?
hard
A. void Awake() {
if (FindObjectsOfType<MusicPlayer>().Length > 1) {
Destroy(gameObject);
} else {
DontDestroyOnLoad(gameObject);
}
}
B. void Awake() {
DontDestroyOnLoad(gameObject);
}
C. void Start() {
Destroy(gameObject);
DontDestroyOnLoad(gameObject);
}
D. void Awake() {
Destroy(gameObject);
}
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.