Loading Scenes in Unity

Siddhant Thakur
Apr 21 · 3 min read

Building off the previous article about a Game Over Screen, how about we add the ability to restart the game once the player looses all his lives. This can be done by simply reloading our given scene.

What is a Scene ?

The place where you work on in Unity is called a scene you can have multiple scenes in a game. Scenes are basically assets, by default one would be on a Sample Scene. You can create a new scene by doing the following,

You can double click on the newly created scene to switch to that scene.

Next thing to do is to add our scene to our project build, this can be done by doing the following,

You can drop your scene into the Scenes in Build. keep track of the number displayed with respect to the scene.

Now let’s create a UI Text which will prompt us on what to do in order to restart the game,

Upon completion you can edit the newly created Text object to get your desired result.

Press R to restart

Now here, since we wish to load a scene, it doesn’t make sense to have the UI reload a new scene it also doesn’t make sense to have the Player load the scene either. So let’s create a Game Manager which will manage everything related to our game which doesn’t fall under any other categories like in this case Loading a scene.

Here GameManager is an empty game object which contains a GameManager behavior script.

The Game Manager behavior is as follows,

Loading a scene is possible through a Unity package called SceneManagement so it is important for us to import that package into the GameManager. This will enable us to use the methods available in the SceneManagement package.

This enables us to use LoadScene method which is responsible for loading the given scene.

It is preferred to go with the scene number instead of scene name.

Where SetGameOver() is called in the UpdatePlayerLives method present in the UIManager script.

where _restartUI is the Text game object variable we had created with the intention of letting the player know that he has an option to restart the game when he presses the desired key. _gameManager is the variable which stores the reference to the GameManager script.

This gives us the following result,

Thank you for reading, more to come!!!

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/. Don’t forget to check out Ask-NFT, a mentorship ecosystem we’ve started

Siddhant Thakur

Written by

Aspiring Game Developer with experience in the field of Machine Learning.

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/. Don’t forget to check out Ask-NFT, a mentorship ecosystem we’ve started

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

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