This content originally appeared on Level Up Coding - Medium and was authored by Dennisse Pagán Dávila
Scenes in Unity can be thought of as unique levels or even separate screens like a main menu. In the previous article, we created a Game Over screen by simply displaying text over the existing Game Scene. However, to restart a game from a game over state, using Unity’s Scene Management is necessary.
In this article, you will learn how to restart a game from a Game Over state!
How to Load Scenes in Unity
- Make sure the scene you want to load is added to your Build Settings. Otherwise, the scene won’t get recognized when you try to access it via code and result in an error.
2. Add the Unity Scene Management library to your script. You can place this in a newly created GameManager Script to help track the state of the game.
The following steps will take place within the same script where the Scene Management was placed
3. Create a bool variable to keep track of the Game Over state.
Note: If you are following this guide to learn how to load scenes in general, your process will be completed in step 4 and this step is skippable . You don’t have to add the game over condition to the input either.
4. In the void Update, set the user input for the key that will restart the game. It can be any key, in my case I’m using “R” for Restart.
Since you have previously added the scene management library, you have access to the SceneManager which will load your desired scene.
The scene is identified by its name, or by its respective scene number. In the example above, the scene is being identified by its number, which is faster than using a string/ the name of the scene.
You can view both in the Build Settings. You can also view and change the scene name from the Project Folder
5. Create a void method to change the value of the bool variable from step 3.
Note: It’s best practice to use methods to change bool variables rather than do it directly.
The following steps will take place within the UI Manager Script which was created in the Score System article, and further used for the Lives Counter, and Game Over Screen.
Alternatively, you can follow along with your own code by applying this logic in the script where it makes the most sense. E.g. A script that can trigger a game over state by handling the player's death or the current of lives the player has.
- Using Script Communication, access the GameManager script from the UI Manager Script or your current script.
2. Call the GameOver Method to the UI Manager Script, or the current script. I created a method to contain everything that happens in my code during the Game Over Screen to avoid clutter.
Note: Make sure the method you are trying to call is public or you will get a protection level error.
3. The GameOver Sequence method gets called when the player's lives are equal to 0 since that is what triggers the game over state.
Now, you should have a fully functional Restart Option!
In the next article, we’ll take a look at how to add explosions to a destroyed game object!
Loading Scenes in Unity was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding - Medium and was authored by Dennisse Pagán Dávila
Dennisse Pagán Dávila | Sciencx (2021-05-04T00:36:37+00:00) Loading Scenes in Unity. Retrieved from https://www.scien.cx/2021/05/04/loading-scenes-in-unity/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.