SceneManager
TypeClass (static members)
NamespaceUnityEngine.SceneManagement
RelatedScene, LoadSceneMode, AsyncOperation

SceneManager

SceneManager loads and unloads scenes: going from the main menu to level 1, restarting a level, or showing a loading screen. Add using UnityEngine.SceneManagement; at the top of your script.

Add scenes to the build firstA scene can only be loaded if it's in the build's scene list: File > Build Profiles > Scene List in Unity 6 (File > Build Settings in older versions). Otherwise you get "Scene 'X' couldn't be loaded because it has not been added to the build settings".

LoadScene(Single) is changing the set in a theater: everything on stage is cleared away and the new set is built. Additive is wheeling an extra set piece onto the stage while the current one stays. LoadSceneAsync is building the next set backstage while the audience watches an intermission (your loading screen), instead of closing the curtain and making everyone wait in silence.

LoadScene("Level2")beforeafterLoadScene("UI", Additive)beforeafterLevel1DontDestroyOnLoadLevel2DontDestroyOnLoadLevel1 is destroyedonly marked objects surviveLevel1Level1UIboth scenes are loaded
Single (the default) unloads everything except objects marked DontDestroyOnLoad. Additive adds the new scene next to what's already loaded, which is useful for UI, lighting or streaming parts of a level.

Static Properties

NameTypeDescription
sceneCountintHow many scenes are currently loaded.

Static Methods

MethodReturnsDescription
LoadScene(string name) / LoadScene(int buildIndex)voidReplace the current scene. Happens at the start of the next frame.
LoadScene(name, LoadSceneMode.Additive)voidLoad on top of the current scene(s).
LoadSceneAsync(name)AsyncOperationLoad in the background. Use for loading screens.
UnloadSceneAsync(name)AsyncOperationUnload an additive scene.
GetActiveScene()SceneThe active scene. .name and .buildIndex are useful.
SetActiveScene(Scene scene)boolChoose which loaded scene new objects get created in.

Events

EventParametersFires when
sceneLoaded(Scene scene, LoadSceneMode mode)A scene finished loading (after its objects' Awake/OnEnable, before Start).
sceneUnloaded(Scene scene)A scene was unloaded.
activeSceneChanged(Scene old, Scene new)The active scene changed.

Code Samples

Menu buttons

using UnityEngine;
using UnityEngine.SceneManagement;

public class MainMenu : MonoBehaviour
{
    // Hook these up to UI Button OnClick events in the Inspector.
    public void PlayGame()   { SceneManager.LoadScene("Level1"); }
    public void QuitGame()   { Application.Quit(); } // does nothing in the Editor

    public void RestartLevel()
    {
        Time.timeScale = 1f; // in case we came from a pause menu
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }

    public void NextLevel()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    }
}

Loading screen with a progress bar

using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class LoadingScreen : MonoBehaviour
{
    [SerializeField] private Slider progressBar;

    public void Load(string sceneName)
    {
        StartCoroutine(LoadRoutine(sceneName));
    }

    IEnumerator LoadRoutine(string sceneName)
    {
        AsyncOperation op = SceneManager.LoadSceneAsync(sceneName);
        op.allowSceneActivation = false; // don't switch until we say so

        // With allowSceneActivation off, progress stops at 0.9 when loading is done.
        while (op.progress < 0.9f)
        {
            progressBar.value = op.progress / 0.9f;
            yield return null;
        }

        progressBar.value = 1f;
        yield return new WaitForSeconds(0.5f); // let the player see 100%
        op.allowSceneActivation = true;        // switch now
    }
}

What happens at runtime

LoadScene("Level1") (Single mode):

  1. The call returns immediately. The rest of your current frame still runs.
  2. At the start of the next frame, every object in the old scene is destroyed (OnDisable, OnDestroy), except those marked DontDestroyOnLoad.
  3. The new scene's objects are created: Awake and OnEnable run, then sceneLoaded fires, then Start runs before their first Update.
  4. Unused assets from the old scene are unloaded from memory.

Synchronous loading freezes the game until finished. For big scenes, use LoadSceneAsync so you can show something while it loads.

Common Mistakes

See also