Nerd For Tech
Published in

Nerd For Tech

Unity Guide

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:

Use a Text element inside the Canvas to let the player know how to restart the game.

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:

A game manager is usually used in Unity to take control of the game flow. As you can see, when we create a script named GameManager, a gear icon appears as it’s a special name in Unity.

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:

The name of the scene is Game.

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:

The build index of the scene is 0.

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.

If you want to know more about me, feel free to connect with me on LinkedIn or visit my website :D



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Fernando Alcantara Santana

Fernando Alcantara Santana

A passionate computer technology engineer and Unity developer that is always looking to grow in every aspect of life :).