Unity-Scene Loading
Objective: Demonstrate how to load different scenes in Unity.
A Unity Scene is a container that holds GameObjects, lighting, environment settings, and game logic. During gameplay, you can load different scenes while the game is running. Doing so offers several advantages, including improved performance, better modularity, easier maintenance, and greater scalability.
For example, you might break your game into different levels to provide unique environments or varying degrees of difficulty. Or you may have a main menu scene that handles user input before the actual gameplay begins. Once the player starts the game, a new scene is loaded to handle the gameplay.
Get Jeff Simon’s stories in your inbox
Join Medium for free to get updates from this writer.
The easiest and most common way to load a scene at runtime in Unity is by using the SceneManager class's LoadScene method. There are several overloads of this method, but we’ll focus on two in this post:
public static void LoadScene(int sceneBuildIndex);
public static void LoadScene(string sceneName);Both of these overloads (as well as all others not listed here) require that the scene be added to your project’s Build Settings in Unity.
With the scenes added to the Build Settings, we can now use SceneManager.LoadScene to load a specific scene. The first overload uses the build index of the scene. For example, you can load a scene with a method like this:
public void CheckForMainMenuRequestWithIndex()
{
if (Input.GetKeyDown(KeyCode.R))
{
// use the index in the Build Settings panel to load the Main_Menu scene
SceneManager.LoadScene(0);
}
}Alternatively, we can load a scene by using its
public void CheckForMainMenuRequestWithSceneName()
{
if (Input.GetKeyDown(KeyCode.M))
{
// use the scene name listed in the Build Settings panel to load the Main_Menu scene
SceneManager.LoadScene("Main_Menu");
}
}Another common action is reloading the current scene. This can be useful for restarting a level or resetting the game state. You can do this by retrieving the currently active scene and loading it again:
public void CheckForReloadCurrentScene()
{
if (Input.GetKeyDown(KeyCode.O))
{
// use the scene name listed from the GetActiveScene() method
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}After calling SceneManager.LoadScene, Unity automatically unloads the current scene if one is already loaded. This process destroys all GameObjects in the scene, except those marked with DontDestroyOnLoad. The new scene is then loaded and activated, with its GameObjects instantiated.
Objects marked with DontDestroyOnLoad persist across scene transitions and will be covered in more detail in a future post. Additionally, static members and singleton instances persist across scene loads, unless explicitly reset or modified in your code.
That’s all for now — thanks for tuning in!








