Andrew Crippen

Jun 2, 2021

2 min read

Loading Scenes in Unity

In this article following my How to Create A Game Over Behavior article I will show how to re-load the scene when the ‘R’ button is pressed.

Unity uses the UnityEngine.SceneManagement namespace to Load Scenes using SceneManagement.LoadScene.

First I created an empty GameObject and named it Game_Manager, I then created and attached a GameManager script to it.

The GameManager script will be in charge of knowing when the game is over and reloading the current scene when the game is over.

In the GameManager class I subscribe and unsubscribe in the OnEnable and OnDisable methods my GameOver() method to my Action named OnPlayerDeath in my Player class. This results in the GameOver() method getting called when the player dies and sets _isGameOver to true.

In my Update() method in my GameManager class I check for input of the ‘R’ key and if the _isGameOver bool true.

If it is then it reloads the current scene, I get the current scene by checking what the Active scene is with GetActiveScene().buildIndex