Reloading Now- Game Dev Series 22
Objective: Make a reloading function when game over.
Previous: It’s Time to Game Over
We got game over now and nothing we can do when this happened.
We should add a restart function to it.
Add an image game object as what we did in the previous articles.
Set the location and size, set the overflow method, and typing the text.
Also, make a variable of Restart_text and set it default as disable in the void Start().
Then set it active when the lives count is 0.
It should shows up when player is game over.
Next we need to make the ‘R’ key actually works.
To reload, we need to use an Unity system called SceneManagement.
Create another game object called Game_Manager in the root of hierarchy. This will be the main part to handle the scene reload.
Create a C# script called GameManager and attach it to Game_Manager. Inside GameManager, we will add the Unity package at the top of script.
In the Update(), we want to reload the game when we press ‘R’.
Add an if statement to reload the scene.
Before we create the line to reload, we should check the scene in Unity.
Back to Unity, select from the dropdown menu File> Build Setting…
Click Add Open Scenes and you should get the current scene name and index.
Now the Unity actually get the scene. We can add a line to it.
We use SceneManager.LoadScene() to reload. LoadScene() can pass in integer or scene name.
In this line, we use SceneManager.GetActiveScene().buildIndex to get the current scene index as the integer.
However, so far we can press R to reload the scene anytime because we have not detect if Player game over or not.
We can create a bool to this situation and create a method to set it true.
Back to UIManager, let’s add a variable and call the method when lives count is 0.
Back to GameManager, add the bool check in the if statement.
And you can reload the game when you run out of lives.