Overview
Games rarely exist as a single scene. Players press Start on the "title screen" to move to the "gameplay" scene, then move to the "results" scene after game over. This flow between multiple scenes creates the game's progression.
Unity provides the SceneManager class specifically for managing this scene navigation. This article covers essential scene transition knowledge—from basic scene loading with SceneManager to asynchronous loading for implementing loading screens.
Adding Scenes to Build Settings
Before loading scenes from scripts, all target scenes must be registered in Build Settings. Forgetting this causes scenes to work in the editor but fail with errors in actual builds.
- Select
File > Build Settings...to open the Build Settings window. - The
Scenes In Buildlist appears. - Drag and drop all scene files (
.unity) your game uses from the Project window into this list.
Added scenes receive build indices: 0, 1, 2... You can load scenes from scripts using either this index number or the scene file name (without extension).
Tip: Build index 0 is typically set to the first scene loaded at game launch (title screen or splash screen).
Basic Scene Loading: LoadScene()
The simplest way to load a scene is SceneManager.LoadScene(). Don't forget to add using UnityEngine.SceneManagement; at the top of your script.
LoadScene() operates synchronously. When called, game execution completely stops until the current scene is destroyed and the new scene finishes loading. For heavy scenes that take time to load, the game may appear to freeze momentarily.
Loading by Scene Name
using UnityEngine;
using UnityEngine.SceneManagement; // Required!
public class TitleScreen : MonoBehaviour
{
public void OnStartButtonClick()
{
// Specify scene file name as string from Build Settings
SceneManager.LoadScene("GameStage1");
}
}
Loading by Build Index
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameController : MonoBehaviour
{
public void GoToTitle()
{
// Load build index 0 scene (title screen)
SceneManager.LoadScene(0);
}
}
Async Loading and Loading Screens: LoadSceneAsync()
To avoid game freezes and show players a "loading screen," asynchronous scene loading is needed. SceneManager.LoadSceneAsync() loads scenes in the background without stopping game execution.
LoadSceneAsync() returns an AsyncOperation object managing load progress. Inspect this object's properties to get loading progress and detect completion.
Here's a simple loading screen implementation:
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI; // Required for UI elements
public class LoadingScreen : MonoBehaviour
{
public Slider progressBar; // UI slider showing load progress
public Text progressText; // UI text showing percentage
public void LoadScene(string sceneName)
{
// Start coroutine for async loading
StartCoroutine(LoadAsynchronously(sceneName));
}
IEnumerator LoadAsynchronously(string sceneName)
{
// Start async scene load with LoadSceneAsync
AsyncOperation operation = SceneManager.LoadSceneAsync(sceneName);
// Loop until isDone is true
while (!operation.isDone)
{
// progress ranges from 0.0 to 0.9 (0.9 means load complete)
float progress = Mathf.Clamp01(operation.progress / 0.9f);
// Update progress bar and text
progressBar.value = progress;
progressText.text = (progress * 100f).ToString("F0") + "%";
// Wait one frame
yield return null;
}
}
}
operation.progress returns 0.9 when loading is 90% complete. The final 10% handles actual scene activation (display switch), so dividing by 0.9f is common for showing 100%.
Summary
Scene transitions are essential for defining game structure.
- Use the
SceneManagerclass for scene transitions. - Always add scenes to Build Settings before loading.
LoadScene(): Simple synchronous load. Game stops during loading.LoadSceneAsync(): Advanced asynchronous load. Essential for loading screens.
While players wait, displaying progress bars or game tips reduces loading stress. Master LoadSceneAsync to improve user experience.