Loading scenes in Unity

Today, we will learn about another element that you will use in your projects, loading scenes. It’s something popular when making games. You can use it when a player completes a level to go to the next stage of your game or to restart your game when he dies.

How to load a scene?

First, we need to add a scene in Building settings. Go to File -> Build Setting. Drag and drop a scene you want to load or click on the “Add Open Scenes” if the scene you want to add is currently open.

Next, we will create a GameObject called “Game_Manager” and a script called “GameManager”. Drag the GameManager script into the GameObject. Open script.

Let’s look at the example in the documentation. As you can see, at the top of the script, we need to add “using UnityEngine.SceneManagement;” to be able to load our scene. Let’s do that.

Loading a scene is pretty simple. We need to use “SceneManager.LoadScene()” method and pass in a scene name or index in Build Settings.

Last thing left to do is call “LoadNextScene()” method!

What if my scene is loading too long?

In some cases, your scene might load for a while and it might look like your game crashed. There are two solutions.

First is making a loading screen, so your player will know what’s happening.

Another thing you could do is to load your scene using SceneManager.LoadSceneAsync(). This method will load your scene in the background while your current scene is still active.