0
0
Unityframework~20 mins

DontDestroyOnLoad usage in Unity - Practice Problems & Coding Challenges

Choose your learning style9 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.