DontDestroyOnLoad Making Duplicates in Unity? Fix Persistent Objects

Illustration: two identical glowing cubes splitting with one fading away

You mark your music player with DontDestroyOnLoad so it survives scene changes. It works, until you go back to the main menu and now two songs play at once. Unity DontDestroyOnLoad duplicates are the classic side effect: the persistent object lives on, and the scene that originally created it loads again and makes another. This guide explains why it happens and shows four reliable fixes: a singleton guard, a bootstrap scene, a runtime-initialized prefab, and a way to handle lost references.

Quick Checklist

  • Does the object exist in a scene you load more than once? Then it needs a guard or must move to a bootstrap scene.
  • Is the object a root GameObject? If not, DontDestroyOnLoad does nothing useful.
  • Does the script destroy gameObject, not just this, when it is the duplicate?
  • Does the persistent object hold references to scene objects that will be destroyed?
  • Can you press Play from any scene and still have your managers? If not, use the startup loader.

Pause the game after a few scene changes and look at the Hierarchy. Count the managers. If there is more than one, you have the duplicate problem, and the rest of this guide fixes it.

Why Duplicates Appear

DontDestroyOnLoad(obj) moves an object into a special scene that Unity does not unload when you load a new one. The object is not saved or “remembered” by your original scene. The original scene still contains its own copy, as an asset in its file.

Here is the sequence that makes duplicates:

  1. Main Menu scene loads. It contains an AudioManager object. Its Awake calls DontDestroyOnLoad.
  2. You load the Game scene. The AudioManager survives.
  3. You return to the Main Menu. The scene loads fresh from disk, including a brand new AudioManager. Now there are two.

Every visit to the menu adds another. The fix is not to prevent DontDestroyOnLoad but to make sure only one instance can ever exist, or to ensure the object is created once from a scene that is loaded only once.

Fix 1: The Singleton Guard

The quick fix is for each new instance to check whether one already exists and destroy itself if so:

using UnityEngine;

public class AudioManager : MonoBehaviour
{
    public static AudioManager Instance { get; private set; }

    void Awake()
    {
        if (Instance != null && Instance != this)
        {
            Destroy(gameObject);
            return;
        }

        Instance = this;
        DontDestroyOnLoad(gameObject);
    }
}

The first instance sets itself as the Instance and persists. When the menu scene reloads and creates a second one, that second copy sees the Instance is already set, destroys itself, and returns before doing anything else. Note the return: without it, the rest of Awake still runs on the doomed object. Also note Destroy(gameObject), not Destroy(this). The latter only removes the component and leaves an empty GameObject behind, which is a surprisingly common bug.

The weak point is that the duplicate still runs its Awake and may run OnEnable and others before it is gone. Keep code that has side effects (starting music, subscribing to events) after the guard, or in Start, which never runs for an object destroyed in Awake. For a fuller treatment of singleton designs and their trade-offs, read the game manager and singleton patterns guide.

Fix 2: A Bootstrap Scene

The cleanest structure is a tiny first scene that creates all persistent objects once and then loads the real game:

  1. Create a scene called Bootstrap. Put your managers in it (AudioManager, GameManager, SaveManager).
  2. Add a script that marks them persistent and loads the main menu.
  3. In File > Build Profiles (or Build Settings), put Bootstrap at index 0.
  4. Never load Bootstrap again.
using UnityEngine;
using UnityEngine.SceneManagement;

public class Bootstrap : MonoBehaviour
{
    [SerializeField] GameObject[] persistentObjects;

    void Awake()
    {
        foreach (var obj in persistentObjects)
            DontDestroyOnLoad(obj);

        SceneManager.LoadScene("MainMenu");
    }
}

Since Bootstrap is only ever loaded once, nothing can create a second copy. The tradeoff: you cannot press Play in the Game scene and have the managers exist. Everything that expects AudioManager.Instance then throws a NullReferenceException. If you want to test any scene directly, combine this with the guard from Fix 1 on each manager, or use Fix 3.

Fix 3: Create It at Startup in Code

You can have the managers created automatically before any scene loads, so no scene needs to contain them. Put a prefab named Managers in a Resources folder, then:

using UnityEngine;

public static class ManagerLoader
{
    [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
    static void Load()
    {
        var prefab = Resources.Load<GameObject>("Managers");
        var instance = Object.Instantiate(prefab);
        instance.name = "Managers";
        Object.DontDestroyOnLoad(instance);
    }
}

The attribute makes Unity call this method once when the game starts, before the first scene loads, including when you press Play from any scene in the editor. The prefab contains every manager as child components. Because it is not part of any scene, scene reloads cannot duplicate it. This is the most robust option, and it works well with the singleton guard as a second layer.

Static state can bite in the editor when Enter Play Mode Options skip the domain reload, since static fields keep their old values between plays. If you use those options, reset your Instance in a method marked [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)].

Root Objects Only

If you see this in the Console:

DontDestroyOnLoad only works for root GameObjects or components on root GameObjects.

then the object you passed is a child of another object. Either unparent it before the call or call it on the root:

transform.SetParent(null);
DontDestroyOnLoad(gameObject);

If the manager lives under a “Managers” parent, call DontDestroyOnLoad on that parent instead, and all the children come along. Calling it on a child and expecting the child to persist while the parent is destroyed does not work.

Lost References After a Scene Change

A persistent object cannot hold Inspector references to objects in the old scene, because those objects are destroyed. A music player that has a reference to a UI slider in the menu scene will show “Missing” or throw a MissingReferenceException after loading the next scene: “The object of type ‘Slider’ has been destroyed but you are still trying to access it.”

The fixes:

  • Let scene objects find the persistent one, not the other way round. The slider’s own script reads AudioManager.Instance in Start and subscribes.
  • Re-acquire references when a scene loads, using SceneManager.sceneLoaded.
  • Always unsubscribe from events in OnDestroy or OnDisable. A persistent object subscribed to a scene button’s event keeps calling into destroyed objects after the scene is gone.
void OnEnable()  => SceneManager.sceneLoaded += OnSceneLoaded;
void OnDisable() => SceneManager.sceneLoaded -= OnSceneLoaded;

void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
    // find what this scene needs
    var camera = Camera.main;
}

A persistent camera or AudioListener is another source of trouble. You may end up with two active AudioListeners and the warning “There are 2 audio listeners in the scene.” Keep exactly one in every loaded scene. If you add scene transitions, the scene fade guide shows a persistent fade canvas that avoids these traps, and the audio manager guide shows a manager that suits this pattern.

📚 Design cleaner code

Managers, singletons and scene architecture get easier with the right fundamentals. See our list of programming and game design books.

Troubleshooting

Two music tracks play after returning to the menu

A second AudioManager was created by the reloaded scene. Add the singleton guard or move creation to a bootstrap scene.

DontDestroyOnLoad only works for root GameObjects or components on root GameObjects

The object has a parent. Call SetParent(null) first or persist the root.

NullReferenceException on Instance when pressing Play in another scene

The manager only exists in the first scene. Use the RuntimeInitializeOnLoadMethod prefab approach so it exists wherever you start.

MissingReferenceException on a persistent object after loading a scene

It still points at an object from the previous scene. Find references again on sceneLoaded or have scene objects register themselves.

The new scene’s object with the same name has different values than the old one

The guard destroyed the new object, so the old one’s data wins. That is correct for managers, but do not put per-scene setup on a manager that gets destroyed. Put it in a separate component.

Destroying the duplicate leaves an empty GameObject

You called Destroy(this) instead of Destroy(gameObject).

FAQ

Should I use DontDestroyOnLoad for everything?

No. Use it for true global services such as audio, input, saving and the game state. Per-level things should live in the scene.

How do I destroy a DontDestroyOnLoad object?

Call Destroy on it like any object. It only survives scene loads, not explicit destruction.

Can I get DontDestroyOnLoad objects in the Hierarchy?

Yes. In Play mode they appear in a separate scene named DontDestroyOnLoad at the bottom of the Hierarchy.

Does DontDestroyOnLoad work with additive scenes?

Yes, the object just moves to the persistent scene. An alternative is a permanent additive “Core” scene that you never unload.

Is a static class better than a persistent object?

For pure data and logic, often yes. You only need a persistent GameObject when you need components such as an AudioSource.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *