Bird
Raised Fist0
Unityframework~20 mins

DontDestroyOnLoad usage in Unity - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
DontDestroyOnLoad Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output when using DontDestroyOnLoad?

Consider the following Unity C# script attached to a GameObject. What will be the output in the console after loading a new scene?

Unity
using UnityEngine;
using UnityEngine.SceneManagement;

public class TestDontDestroy : MonoBehaviour
{
    void Awake()
    {
        DontDestroyOnLoad(gameObject);
        Debug.Log("Object Awake");
    }

    void Start()
    {
        Debug.Log("Object Start");
        SceneManager.LoadScene("Scene2");
    }

    void OnEnable()
    {
        Debug.Log("Object Enabled");
    }
}
A"Object Awake\nObject Enabled\nObject Start" printed twice
B"Object Awake\nObject Enabled\nObject Start" printed once
COnly "Object Awake" printed once
DNo output printed because scene loads immediately
Attempts:
2 left
💡 Hint

Think about what DontDestroyOnLoad does to the GameObject when a new scene loads.

🧠 Conceptual
intermediate
1:00remaining
What does DontDestroyOnLoad do in Unity?

Which of the following best describes the purpose of DontDestroyOnLoad in Unity?

AIt prevents a GameObject from being destroyed when loading a new scene.
BIt disables a GameObject permanently in the current scene.
CIt reloads the current scene without destroying any GameObjects.
DIt destroys all GameObjects except the one it is called on.
Attempts:
2 left
💡 Hint

Think about what happens to GameObjects normally when a new scene loads.

🔧 Debug
advanced
2:30remaining
Why does this GameObject duplicate after scene loads?

Look at this code snippet. Why does the GameObject duplicate after loading a new scene multiple times?

Unity
using UnityEngine;
using UnityEngine.SceneManagement;

public class PersistentObject : MonoBehaviour
{
    void Awake()
    {
        DontDestroyOnLoad(gameObject);
    }

    void Start()
    {
        SceneManager.LoadScene("Scene2");
    }
}
ABecause Awake is not called when loading a new scene.
BBecause DontDestroyOnLoad destroys the object after scene load causing a new one to be created.
CBecause SceneManager.LoadScene unloads all objects including those marked DontDestroyOnLoad.
DBecause the script does not check if an instance already exists before calling DontDestroyOnLoad.
Attempts:
2 left
💡 Hint

Think about what happens if the scene with this script is loaded multiple times.

📝 Syntax
advanced
1:30remaining
Identify the syntax error in DontDestroyOnLoad usage

Which option contains a syntax error when trying to use DontDestroyOnLoad?

Unity
using UnityEngine;

public class Example : MonoBehaviour
{
    void Awake()
    {
        // Mark this GameObject to not be destroyed on scene load
        DontDestroyOnLoad(gameObject);
    }
}
ADontDestroyOnLoad(gameObject);
BDontDestroyOnLoad(this);
CDontDestroyOnLoad(gameObject) // missing semicolon
DDontDestroyOnLoad(gameObject); // correct usage
Attempts:
2 left
💡 Hint

Check for missing punctuation in the code.

🚀 Application
expert
3:00remaining
How to prevent multiple persistent GameObjects using DontDestroyOnLoad?

You want to create a GameObject that persists across scenes without duplicates. Which code snippet correctly prevents duplicates?

A
void Awake() {
  if (FindObjectsOfType<PersistentObject>().Length > 1) {
    Destroy(gameObject);
  } else {
    DontDestroyOnLoad(gameObject);
  }
}
B
void Awake() {
  DontDestroyOnLoad(gameObject);
}
C
void Awake() {
  if (GameObject.Find("PersistentObject") == null) {
    DontDestroyOnLoad(gameObject);
  }
}
D
void Awake() {
  Destroy(gameObject);
  DontDestroyOnLoad(gameObject);
}
Attempts:
2 left
💡 Hint

Think about how to check if another instance already exists before marking DontDestroyOnLoad.

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

  1. Step 1: Understand what DontDestroyOnLoad does

    This method prevents a GameObject from being destroyed when a new scene loads.
  2. Step 2: Identify the purpose in scene management

    It keeps important objects like music players or managers alive across scenes.
  3. Final Answer:

    To keep a GameObject alive when switching scenes -> Option D
  4. 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

  1. Step 1: Identify the correct parameter for DontDestroyOnLoad

    The method requires a GameObject to keep alive, usually referenced as gameObject in scripts.
  2. 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.
  3. Final Answer:

    DontDestroyOnLoad(gameObject); -> Option C
  4. 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:
void Awake() {
    DontDestroyOnLoad(gameObject);
}

void Start() {
    Debug.Log("Object alive");
}
What will happen when the scene changes?
medium
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

  1. 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.
  2. 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.
  3. Final Answer:

    The GameObject will persist and "Object alive" will print once -> Option A
  4. 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

  1. Step 1: Identify the parameter type for DontDestroyOnLoad

    DontDestroyOnLoad requires a GameObject, but 'this' refers to the script instance, not the GameObject.
  2. Step 2: Correct the parameter

    Use gameObject to pass the GameObject the script is attached to.
  3. Final Answer:

    Error: 'this' is not a GameObject; fix by using DontDestroyOnLoad(gameObject); -> Option B
  4. 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

  1. Step 1: Understand the problem of duplicates

    Using DontDestroyOnLoad alone keeps the object alive, but reloading the scene creates duplicates.
  2. 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.
  3. 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.
  4. Final Answer:

    void Awake() { if (FindObjectsOfType<MusicPlayer>().Length > 1) { Destroy(gameObject); } else { DontDestroyOnLoad(gameObject); } } -> Option A
  5. Quick Check:

    Check duplicates before DontDestroyOnLoad to avoid duplicates = B [OK]
Hint: Check existing objects before DontDestroyOnLoad to prevent duplicates [OK]
Common Mistakes:
  • Not checking for duplicates causing multiple music players
  • Destroying object before calling DontDestroyOnLoad
  • Calling DontDestroyOnLoad without duplicate check
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard