Loading Scenes in Unity
Scenes = Leves = Menus = and more
Objective: Reset the game when the users press the ‘R’ key
In Unity we can create levels, menus and screens using Scenes. In every Unity Project you will need at least one scene. The scene is the space that contains the game objects of your project to be used.
In this example we’re gonna use the game scene of the space shooter project and load the same scene to reset the game when the player dies.
So first we have to create a Game_Manager empty object and add a GameManager Script. Then we have to open the game manager script and declare a _isGameOver bool variable to use as a flag and create a public function FinishGame to change the value of that variable.
Before creating the function to load the scene we have to go to the build setting drop the scene to the window.
Now in the game manager script we’re gonna create a function to reset the game. First we have to use the namespace UnityEngine.SceneManagement, then create the public function reset game and use the function SceneManager.LoadScene passing the index of the scene as a parameter. Then in the Update method we have to check for the user input and for the value of the _isGameOver variable.
Now in the player script we have to call the FinishGame when the player dies.
And that’s it we have a functional reset game function loading the same game scene.