Loading scenes in Unity
A quick guide about how to load scenes in Unity
Objective: Load a scene of a space shooter game in Unity to restart the game.
Now that we’ve already implemented a retro game over behavior in the space shooter game, let’s add a feature to restart the game when the player gets destroyed.
In order to add the feature, let’s create a new UI element to let the player know how to restart the level when the game is over. One way to restart the game is loading the same game scene and we can trigger it when the player presses a key:
Now, to implement the restart feature, we need to create a new script to control the game flow. Let’s create an empty gameobject and attach the new script:
Loading the scene
Open the Game Manager script and add the SceneManagement class at the top of the script. We’ll need this class to handle the scene management at run-time.
Then, let’s create a new bool to know if the game is over already or not:
Then, to change the bool value when the game is over, let’s create a public function:
Now, to handle the input form the player, let’s add a condition inside the Update function that will check if the R key was pressed and that the game is already over:
To reload the scene we can use the LoadScene method from the SceneManager class, but we need to indicate the right parameter to load the desired scene. We can choose the scene by:
- Sending the name or path of the scene.
- Sending the build index of the scene (found in build settings).
Note: We could also use the LoadSceneAsync method to avoid performance issues while loading the scene, but I’ll probably cover it in another post.
Loading the scene by name
To load the scene using its name, we need to identify the name in Unity and send a string in the LoadScene method:
Loading the scene by build index
To load the scene by build index, we need to make sure that the scene is included inside the build settings. We can choose to add the current opened scene with the button at the right or we can drag it from the files into the build settings. Once we add the scene, we’ll see the build index at the right and we need to send it in the LoadScene method:
Executing the scene load
Now, to trigger the scene load, we can open the UI Manager script and create a new variable to store a reference to the Game Manager script component attached to the game manager gameobject:
Let’s find and assign the script component reference inside the Start function to the variable and check if it’s null to print an error message as good practice:
Finally, in the function that shows the game over warning, let’s call the GameOver function from the Game Manager script to indicate that the game is over:
Now, if we run the game in Unity, we’ll see that we can reload the game scene when we press the R key after the player gets destroyed:
And that’s it, you can load your scenes in Unity! :D. I’ll see you in the next post, where I’ll be showing how to create enemy explosions in the space shooter game with Unity.