# DontDestroyOnLoad(Object)

> Do not destroy the target Object when loading a new Scene.

## Definition

* **Type:** Method
* **Namespace:** [UnityEngine](/engine/6000.5/script-reference/unityengine.md)
* **Assembly:** UnityEngine.CoreModule

```csharp
public static void DontDestroyOnLoad(Object target)
```

### Parameters

**** (\[Object]\(/engine/6000.5/script-reference/unityengine/object)): An [Object](/engine/6000.5/script-reference/unityengine/object.md) not destroyed on [Scene](/engine/6000.5/script-reference/unityeditor/searchservice/scene.md) change.

### Remarks

The load of a new [Scene](/engine/6000.5/script-reference/unityeditor/searchservice/scene.md) destroys all current [Scene](/engine/6000.5/script-reference/unityeditor/searchservice/scene.md) objects. Call [Object.DontDestroyOnLoad](/engine/6000.5/script-reference/unityengine/object/dontdestroyonload.md) to preserve an [Object](/engine/6000.5/script-reference/unityengine/object.md) during scene loading. If the target [Object](/engine/6000.5/script-reference/unityengine/object.md) is a component or [GameObject](/engine/6000.5/script-reference/unityengine/gameobject.md), Unity also preserves all of the [Transform](/engine/6000.5/script-reference/unityengine/transform.md)’s children. [Object.DontDestroyOnLoad](/engine/6000.5/script-reference/unityengine/object/dontdestroyonload.md) only works for root GameObjects or components on root GameObjects. [Object.DontDestroyOnLoad](/engine/6000.5/script-reference/unityengine/object/dontdestroyonload.md) does not return a value.

The following example script uses [Object.DontDestroyOnLoad](/engine/6000.5/script-reference/unityengine/object/dontdestroyonload.md). The example has `scene1` which starts playing background music from an [AudioSource](/engine/6000.5/script-reference/unityengine/audiosource.md). The music continues when `scene2` loads. Switch between scenes using a button.

To implement this example, create two new [Scene](/engine/6000.5/script-reference/unityeditor/searchservice/scene.md)s, named `scene1` and `scene2`.  Open `scene1` and add the `SceneSwap.cs` script to an empty [GameObject](/engine/6000.5/script-reference/unityengine/gameobject.md) and name it `Menu`.  Next, add `DontDestroy.cs` to a new [GameObject](/engine/6000.5/script-reference/unityengine/gameobject.md) and name it `BackgroundMusic`. Add an [AudioSource](/engine/6000.5/script-reference/unityengine/audiosource.md) to `BackgroundMusic` - `Add Component > Audio > Audio Source` - and import an [AudioClip](/engine/6000.5/script-reference/unityengine/audioclip.md) into your Project. Assign the [AudioClip](/engine/6000.5/script-reference/unityengine/audioclip.md) to the [AudioSource](/engine/6000.5/script-reference/unityengine/audiosource.md)’s [AudioClip](/engine/6000.5/script-reference/unityengine/audioclip.md) field.  Create a tag, call it `music`, and add it to `BackgroundMusic`. Switch to `scene2`. Again add `SceneSwap.cs` to a new [GameObject](/engine/6000.5/script-reference/unityengine/gameobject.md) and name it `Menu`.  Save the Project. Return to `scene1` and run the Project from the `Editor`.

`SceneSwap.cs` script:

```csharp
using UnityEngine;
using UnityEngine.SceneManagement;

// Object.DontDestroyOnLoad example.
//
// Two scenes call each other. This happens when OnGUI button is clicked.
// scene1 will load scene2; scene2 will load scene1. Both scenes have
// the Menu GameObject with the SceneSwap.cs script attached.
//
// AudioSource plays an AudioClip as the game runs. This is on the
// BackgroundMusic GameObject which has a music tag.  The audio
// starts in AudioSource.playOnAwake. The DontDestroy.cs script
// is attached to BackgroundMusic.

public class SceneSwap : MonoBehaviour
{
    private void OnGUI()
    {
        int xCenter = (Screen.width / 2);
        int yCenter = (Screen.height / 2);
        int width = 400;
        int height = 120;

        GUIStyle fontSize = new GUIStyle(GUI.skin.GetStyle("button"));
        fontSize.fontSize = 32;

        Scene scene = SceneManager.GetActiveScene();

        if (scene.name == "scene1")
        {
            // Show a button to allow scene2 to be switched to.
            if (GUI.Button(new Rect(xCenter - width / 2, yCenter - height / 2, width, height), "Load second scene", fontSize))
            {
                SceneManager.LoadScene("scene2");
            }
        }
        else
        {
            // Show a button to allow scene1 to be returned to.
            if (GUI.Button(new Rect(xCenter - width / 2, yCenter - height / 2, width, height), "Return to first scene", fontSize))
            {
                SceneManager.LoadScene("scene1");
            }
        }
    }
}
```

`DontDestroy.cs` script:

```csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// Object.DontDestroyOnLoad example.
//
// This script example manages the playing audio. The GameObject with the
// "music" tag is the BackgroundMusic GameObject. The AudioSource has the
// audio attached to the AudioClip.

public class DontDestroy : MonoBehaviour
{
    void Awake()
    {
        GameObject[] objs = GameObject.FindGameObjectsWithTag("music");

        if (objs.Length > 1)
        {
            Destroy(this.gameObject);
        }

        DontDestroyOnLoad(this.gameObject);
    }
}
```
