# SceneManager

> Manage scenes in the Player and in Play mode in the Editor.

## Definition

* **Type:** Class
* **Namespace:** [UnityEngine.SceneManagement](/engine/6000.7/script-reference/unityengine/scenemanagement.md)
* **Assembly:** UnityEngine.CoreModule

```csharp
public class SceneManager
```

## Remarks

You can use the SceneManager to manage and manipulate scenes in the Player.

**Scene creation, loading and unloading**

* To create scenes dynamically at runtime, use [SceneManager.CreateScene](/engine/6000.7/script-reference/unityengine/scenemanagement/scenemanager/createscene.md).
* To load scenes from built content, use [SceneManager.LoadSceneAsync](/engine/6000.7/script-reference/unityengine/scenemanagement/scenemanager/loadsceneasync.md) or [SceneManager.LoadScene](/engine/6000.7/script-reference/unityengine/scenemanagement/scenemanager/loadscene.md).
* Scenes cannot be saved at runtime.
* You can load multiple scenes simultaneously. To add more scenes to the currently open ones, use the [LoadSceneMode.Additive](/engine/6000.7/script-reference/unityengine/scenemanagement/loadscenemode/additive.md) option when calling [SceneManager.LoadSceneAsync](/engine/6000.7/script-reference/unityengine/scenemanagement/scenemanager/loadsceneasync.md) or [SceneManager.LoadScene](/engine/6000.7/script-reference/unityengine/scenemanagement/scenemanager/loadscene.md).
* You can load the same scene multiple times in both the Player and in Play mode in the Editor.  In Edit mode in the Editor, SceneManager cannot be used and a scene can only be loaded once (using [EditorSceneManager](/engine/6000.7/script-reference/unityeditor/scenemanagement/editorscenemanager.md)).
* To unload a scene explicitly, use [SceneManager.UnloadSceneAsync](/engine/6000.7/script-reference/unityengine/scenemanagement/scenemanager/unloadsceneasync.md).  All open scenes can be unloaded implicitly by loading another scene with [LoadSceneMode.Single](/engine/6000.7/script-reference/unityengine/scenemanagement/loadscenemode/single.md).

**Accessing loaded scenes**

The Scene Manager offers APIs to access currently loaded Scenes. For example, [SceneManager.loadedSceneCount](/engine/6000.7/script-reference/unityengine/scenemanagement/scenemanager/loadedscenecount.md), [SceneManager.GetSceneAt](/engine/6000.7/script-reference/unityengine/scenemanagement/scenemanager/getsceneat.md), and [SceneManager.GetSceneByPath](/engine/6000.7/script-reference/unityengine/scenemanagement/scenemanager/getscenebypath.md).

**Scene manipulation**

To move objects between scenes, use methods like [SceneManager.MergeScenes](/engine/6000.7/script-reference/unityengine/scenemanagement/scenemanager/mergescenes.md) and [SceneManager.MoveGameObjectToScene](/engine/6000.7/script-reference/unityengine/scenemanagement/scenemanager/movegameobjecttoscene.md).

**SceneManager events**

The SceneManager also exposes the following events:

* [SceneManager.activeSceneChanged](/engine/6000.7/script-reference/unityengine/scenemanagement/scenemanager/activescenechanged.md)
* [SceneManager.sceneLoaded](/engine/6000.7/script-reference/unityengine/scenemanagement/scenemanager/sceneloaded.md)
* [SceneManager.sceneUnloaded](/engine/6000.7/script-reference/unityengine/scenemanagement/scenemanager/sceneunloaded.md)

Scripts can register on these events and then be notified when there are changes in the state of the SceneManager.

**The scene list**

The Player contains a BuildSettings object which records the list of scenes that are available to load.  The contents of this list is exposed by [SceneManager.sceneCountInBuildSettings](/engine/6000.7/script-reference/unityengine/scenemanagement/scenemanager/scenecountinbuildsettings.md) and [SceneUtility.GetScenePathByBuildIndex](/engine/6000.7/script-reference/unityengine/scenemanagement/sceneutility/getscenepathbybuildindex.md).

The contents of this list is determined when the Player is built:

* By default, all the enabled scenes in the [EditorBuildSettings.scenes](/engine/6000.7/script-reference/unityeditor/editorbuildsettings/scenes.md) array are included. You can view and edit this list from the active profile in the Build Profiles window.
* When building via scripts with [BuildPipeline.BuildPlayer](/engine/6000.7/script-reference/unityeditor/buildpipeline/buildplayer.md), specify scenes using [BuildPlayerOptions.scenes](/engine/6000.7/script-reference/unityeditor/buildplayeroptions/scenes.md).

The scene order is crucial for several reasons:

* The first enabled scene in the Scene list (with a build index of 0) loads automatically when the Player starts.
* Earlier listed Scenes load faster due to optimized assignment of their dependent content to fewer sharedAsset files.
* [SceneManager.LoadSceneAsync](/engine/6000.7/script-reference/unityengine/scenemanagement/scenemanager/loadsceneasync.md) and [SceneManager.LoadScene](/engine/6000.7/script-reference/unityengine/scenemanagement/scenemanager/loadscene.md) supports loading scenes by index, determined by the order in [EditorBuildSettings.scenes](/engine/6000.7/script-reference/unityeditor/editorbuildsettings/scenes.md) or [BuildPlayerOptions.scenes](/engine/6000.7/script-reference/unityeditor/buildplayeroptions/scenes.md) after any disabled scenes are removed.

**AssetBundles and Scenes**

* Additional scenes can be included in AssetBundles. When an [AssetBundle](/engine/6000.7/script-reference/unityengine/assetbundle.md) that contains scenes is loaded, its scenes become available to the SceneManager and can be loaded by path using [SceneManager.LoadSceneAsync](/engine/6000.7/script-reference/unityengine/scenemanagement/scenemanager/loadsceneasync.md) or [SceneManager.LoadScene](/engine/6000.7/script-reference/unityengine/scenemanagement/scenemanager/loadscene.md).
* Scenes from AssetBundles have a buildIndex of -1.
* When loading scenes by path, a match from loaded AssetBundle takes priority over scenes in the Player build.

**Scene management in the Editor**

* Use [EditorSceneManager](/engine/6000.7/script-reference/unityeditor/scenemanagement/editorscenemanager.md) instead of SceneManager for scene authoring and manipulation in the Editor.
* The [SceneManager](/engine/6000.7/script-reference/unityengine/scenemanagement/scenemanager.md) API should only be used in Play mode.  In Edit mode calls to unsupported methods such as [SceneManager.LoadSceneAsync](/engine/6000.7/script-reference/unityengine/scenemanagement/scenemanager/loadsceneasync.md) will throw an invalid operation exception.
* In Play mode, only scenes listed in [EditorBuildSettings](/engine/6000.7/script-reference/unityeditor/editorbuildsettings.md) are available to load, along with scenes from loaded AssetBundles, simulating Player behavior.

**Notes**

* Loading Scenes by index can be fragile due to potential reordering; the recommended best practice is to load scenes by path for better clarity.
* Loading scenes by filename (without a full path) can cause issues if multiple scenes share the same name; full path specification removes that ambiguity.

Additional Resources: [EditorSceneManager](/engine/6000.7/script-reference/unityeditor/scenemanagement/editorscenemanager.md), [SceneUtility](/engine/6000.7/script-reference/unityengine/scenemanagement/sceneutility.md), buildIndex, [EditorBuildSettingsScene.enabled](/engine/6000.7/script-reference/unityeditor/editorbuildsettingsscene/enabled.md), [AssetBundle.GetAllScenePaths](/engine/6000.7/script-reference/unityengine/assetbundle/getallscenepaths.md).

## Examples

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

// This MonoBehaviour could be placed as a component inside the first scene in the Build Profiles Scene List.
// When the Player starts it instantiates this MonoBehaviour, which in turn loads
// an additional scene.
public class SceneLoader : MonoBehaviour
{
    // This scene must be listed in the Scene List in the Build Profiles Window,
    // or available from a loaded AssetBundle.
    const string sceneToLoad = "Assets/Example/AnotherScene.unity";

    void Start()
    {
        var op = SceneManager.LoadSceneAsync(sceneToLoad, LoadSceneMode.Additive);
        op.completed += (AsyncOperation obj) =>
        {
            Scene loadedScene = SceneManager.GetSceneByPath(sceneToLoad);
            Debug.Log($"{sceneToLoad} finished loading (build index: {loadedScene.buildIndex}).");
            Debug.Log($"It has {loadedScene.rootCount} root(s).");
            Debug.Log($"There are now {SceneManager.loadedSceneCount} Scenes open.");
        };
    }

    private void OnDestroy()
    {
        // When closing the Scene containing this MonoBehaviour we also remove the Scene we loaded
        SceneManager.UnloadSceneAsync(sceneToLoad);
    }
}
```

```csharp
using System.Text;
using UnityEngine;
using UnityEngine.SceneManagement;

public class SceneInfo : MonoBehaviour
{
    void Start()
    {
        LogSceneManagerState();
    }

    void LogSceneManagerState()
    {
        var sb = new StringBuilder();
        sb.AppendLine("SceneManager state");

        sb.AppendLine($"Active Scene: {SceneManager.GetActiveScene().path}");

        sb.AppendLine($"Scene List (size {SceneManager.sceneCountInBuildSettings})");
        for(int i = 0; i < SceneManager.sceneCountInBuildSettings; i++)
        {
            var scenePath = SceneUtility.GetScenePathByBuildIndex(i);
            sb.AppendLine($"  {i}: {scenePath}");
        }

        sb.AppendLine($"Loaded Scenes (size {SceneManager.sceneCount})");
        for(int i = 0; i < SceneManager.sceneCount; i++)
        {
            var scene = SceneManager.GetSceneAt(i);
            sb.AppendLine($"  {i}: {scene.path}");
        }

        Debug.Log(sb.ToString());
    }
}
```

## Static Properties

| Property                                                                                                                           | Description                         |
| ---------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------- |
| [loadedSceneCount](/engine/6000.7/script-reference/unityengine/scenemanagement/scenemanager/loadedscenecount.md)                   | The number of loaded Scenes.        |
| [sceneCount](/engine/6000.7/script-reference/unityengine/scenemanagement/scenemanager/scenecount.md)                               | The current number of Scenes.       |
| [sceneCountInBuildSettings](/engine/6000.7/script-reference/unityengine/scenemanagement/scenemanager/scenecountinbuildsettings.md) | Number of Scenes in Build Settings. |

## Static Methods

| Method                                                                                                                             | Description                                                                                                                                                                                                                                  |
| ---------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [CreateScene](/engine/6000.7/script-reference/unityengine/scenemanagement/scenemanager/createscene.md)                             | Create an empty new Scene at runtime with the given name.                                                                                                                                                                                    |
| [GetActiveScene](/engine/6000.7/script-reference/unityengine/scenemanagement/scenemanager/getactivescene.md)                       | Gets the currently active Scene.                                                                                                                                                                                                             |
| [GetSceneAt](/engine/6000.7/script-reference/unityengine/scenemanagement/scenemanager/getsceneat.md)                               | Gets the scene at the specified index in the SceneManager's scene list. This includes scenes that are currently loading or unloading.                                                                                                        |
| [GetSceneByBuildIndex](/engine/6000.7/script-reference/unityengine/scenemanagement/scenemanager/getscenebybuildindex.md)           | Get a Scene struct from a build index.                                                                                                                                                                                                       |
| [GetSceneByLoadableSceneId](/engine/6000.7/script-reference/unityengine/scenemanagement/scenemanager/getscenebyloadablesceneid.md) | The [Scene](/engine/6000.7/script-reference/unityeditor/searchservice/scene.md) handle for a scene that is already loaded and originated from the given [LoadableSceneId](/engine/6000.7/script-reference/unity/loading/loadablesceneid.md). |
| [GetSceneByName](/engine/6000.7/script-reference/unityengine/scenemanagement/scenemanager/getscenebyname.md)                       | Searches through the Scenes loaded for a Scene with the given name.                                                                                                                                                                          |
| [GetSceneByPath](/engine/6000.7/script-reference/unityengine/scenemanagement/scenemanager/getscenebypath.md)                       | Searches all Scenes loaded for a Scene that has the given asset path.                                                                                                                                                                        |
| [LoadScene](/engine/6000.7/script-reference/unityengine/scenemanagement/scenemanager/loadscene.md)                                 | Loads the Scene by its name or index in Build Settings.                                                                                                                                                                                      |
| [LoadSceneAsync](/engine/6000.7/script-reference/unityengine/scenemanagement/scenemanager/loadsceneasync.md)                       | Loads the Scene asynchronously in the background.                                                                                                                                                                                            |
| [MergeScenes](/engine/6000.7/script-reference/unityengine/scenemanagement/scenemanager/mergescenes.md)                             | This will merge the source Scene into the destinationScene.                                                                                                                                                                                  |
| [MoveGameObjectsToScene](/engine/6000.7/script-reference/unityengine/scenemanagement/scenemanager/movegameobjectstoscene.md)       | Move multiple GameObjects, represented by a NativeArray of instance IDs, from their current Scene to a new Scene.                                                                                                                            |
| [MoveGameObjectToScene](/engine/6000.7/script-reference/unityengine/scenemanagement/scenemanager/movegameobjecttoscene.md)         | Move a GameObject from its current Scene to a new Scene.                                                                                                                                                                                     |
| [SetActiveScene](/engine/6000.7/script-reference/unityengine/scenemanagement/scenemanager/setactivescene.md)                       | Set the Scene to be active.                                                                                                                                                                                                                  |
| [UnloadSceneAsync](/engine/6000.7/script-reference/unityengine/scenemanagement/scenemanager/unloadsceneasync.md)                   | Destroys all GameObjects associated with the given Scene and removes the Scene from the SceneManager.                                                                                                                                        |

## Static Events

| Member                                                                                                               | Description                                                                          |
| -------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------ |
| [activeSceneChanged](/engine/6000.7/script-reference/unityengine/scenemanagement/scenemanager/activescenechanged.md) | Subscribe to this event to get notified when the active Scene has changed.           |
| [sceneLoaded](/engine/6000.7/script-reference/unityengine/scenemanagement/scenemanager/sceneloaded.md)               | Assign a custom callback to this event to get notifications when a Scene has loaded. |
| [sceneUnloaded](/engine/6000.7/script-reference/unityengine/scenemanagement/scenemanager/sceneunloaded.md)           | Add a delegate to this to get notifications when a Scene has unloaded.               |
