And…..Scene! Loading Scenes in Unity
We’ve implemented our Game Over text when we’ve run out of lives but we want the player to be able to restart the game. Let’s do this!
First off we’ll create a new Text Element in the Canvas that gives the user the instructions on how to restart the game by pressing the R key.
Next it’s good practice to create a Game Manager for you game to handle these types of actions. Create an empty Object and call it Game_Manager. Next in your script folder create a corresponding new script called GameManager. You’ll notice that Unity gives it a gear icon. So it knows it’s a thing :) Drag it onto your Game_Manager Object
Next we have to script the behavior in our game manager. We start by declaring the use of UnityEngine.SceneManagment. We’ll also create a variable to set when the GameOverSequence is called on….you guessed it when the game is over!
In our update method we will check for the R key and the _isGameOver state to be true and when those two conditions are true we will use the SceneManage.LoadScene(1) to reload our Game Scene. (the 1 is an index of all of a games scenes and they can be reviewed by going into Build settings and checking for their index number).
And just like that we’ve reloaded our scene. All our lives are reset as well as our score starts from 0 again.
Next up we will make things go boom when we create enemy explosions when they are hit. Until then…Adios!