| Type | Class (static members) |
|---|---|
| Namespace | UnityEngine.SceneManagement |
| Related | Scene, 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.
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.
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
| Name | Type | Description |
|---|---|---|
sceneCount | int | How many scenes are currently loaded. |
Static Methods
| Method | Returns | Description |
|---|---|---|
LoadScene(string name) / LoadScene(int buildIndex) | void | Replace the current scene. Happens at the start of the next frame. |
LoadScene(name, LoadSceneMode.Additive) | void | Load on top of the current scene(s). |
LoadSceneAsync(name) | AsyncOperation | Load in the background. Use for loading screens. |
UnloadSceneAsync(name) | AsyncOperation | Unload an additive scene. |
GetActiveScene() | Scene | The active scene. .name and .buildIndex are useful. |
SetActiveScene(Scene scene) | bool | Choose which loaded scene new objects get created in. |
Events
| Event | Parameters | Fires 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):
- The call returns immediately. The rest of your current frame still runs.
- At the start of the next frame, every object in the old scene is destroyed (
OnDisable,OnDestroy), except those markedDontDestroyOnLoad. - The new scene's objects are created:
AwakeandOnEnablerun, thensceneLoadedfires, thenStartruns before their firstUpdate. - 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
- Scene not in the build list. See the warning at the top.
- Frozen after restart.
Time.timeScalewas 0 from the pause menu; it doesn't reset on scene load. - Losing data between scenes. Everything is destroyed. Use a
DontDestroyOnLoadmanager,staticfields, a ScriptableObject, or PlayerPrefs to carry data over. - Forgetting the using. "The name 'SceneManager' does not exist" means you're missing
using UnityEngine.SceneManagement;.
See also
This page is a simplified, AI-assisted interpretation. For the complete and authoritative reference, see the official Unity documentation.