Using LoadScene and LoadSceneAsync in Unity
.avif?height=300)
Important Declarations
In this section, I enumerate the most relevant declarations related to loading a scene in Unity. The Unity API docs include additional declarations.
public static void LoadScene(
int sceneBuildIndex,
SceneManagement.LoadSceneMode mode = LoadSceneMode.Single
);
public static void LoadScene(
string sceneName,
SceneManagement.LoadSceneMode mode = LoadSceneMode.Single
);
public static AsyncOperation LoadSceneAsync(
int sceneBuildIndex,
SceneManagement.LoadSceneMode mode = LoadSceneMode.Single
);
public static AsyncOperation LoadSceneAsync(
string sceneName,
SceneManagement.LoadSceneMode mode = LoadSceneMode.Single
);
What you will learn
In this article, you will learn how to load a scene in Unity. By the end of this article, you will feel comfortable using the Unity SceneManager class to load scenes using the LoadScene and LoadSceneAsync methods.
What you need to know
- Use LoadSceneAsync.
- LoadScene is a synchronous operation and will cause freezes and other performance issues.
- LoadSceneAsync is an AsyncOperation and will not freeze your game.
Unity SceneManager
LoadScene and LoadSceneAsync are static methods of the SceneManager class. You need to import the SceneManagement namespace to your class. This lets you access the SceneManager class methods.
To import the namespace, add using UnityEngine.SceneManagement to the top of your class, before any namespace declarations.
You now have access to the LoadScene and LoadSceneAsync methods.
Both of these methods are static methods of the SceneManager class. Therefore, you will always need to call them by first writing out the class reference, like this:
SceneManager.LoadScene(...)
SceneManager.LoadSceneAsync(...)
Identify your .Unity Scene
Options to Identify a .Unity Scene
Both LoadScene and LoadSceneAsync expect you to provide either an int sceneBuildIndex or a string sceneName as an input argument. This is a unique identifier for your scene.
What is a SceneBuildIndex?
The sceneBuildIndex is a unique integer assigned to each scene included in the build. The sceneBuildIndex depends on the order of the scenes in your build. This is defined in your scene build settings.
To open your build settings, click Toolbar -> File -> Build Settings.
The scene build index is the number to the right of each scene included in the build settings. This number will change if you move the scene up or down in the list.
What is a SceneName?
The sceneName is a unique file path associated with the scene file included in your build. The scene name can accept just the filename without extension (e.g., “MyScene.unity” -> “MyScene”). But, this does not work consistently if you have multiple scenes with the same name.
Create a free account, or log in.
Gain access to free articles, game development tools, and game assets.