2D Galaxy Shooter — Pressing the R Key to Restart the Game After Death
Today, I will go over how to make new scenes and load scenes to simulate a game restart! First, let’s create a new scene for starting the game. This makes it so you aren’t just thrown into enemies right off the bat.
Creating a New Scene
- If you don’t already have a “Scenes” folder in your Assets folder, create one now.
- Create a new scene in the Scenes folder.
- Rename it “Starting_Scene”.
- Select the Camera and look at the Camera component.
- Change flags to Solid Color, and the Background to black.
- Get the picture you used for the background and drag it into the scene.
- Save the scene, then head back to the game scene.
- Select the Background there, and head to the Transform component.
- At the “3 dot” drop down menu, click “Copy component”.
- Head back to the Starting_Scene.
- Select the Background there, then hit the 3 dot menu, then select the “Paste Component”. This should make the background look exactly like the one in the Game scene.
Starting Game UI Countdown
Much like making the GAME OVER text flicker in my last article, let’s use coroutines!
- Make a Starting_Game_UI script and drag it onto Canvas.
- Make 2 new Text UI objects:
- At the top, there is a library we need to add: SceneManagement.
- This will allow us to change scenes.
Reference the Countdown_Text in the Starting_Game_UI script under Canvas.
With the coroutine like this, it’s kind of like giving the player 4 seconds to get ready. Look at the above .GIF for reference.
As soon as the coroutine finishes in this script, it starts the “Game” scene.
Build Settings in Unity
- While in the Game scene, click file, then “Build Settings…”.
- Now click the “Add Open Scenes” button, that will add the current scene to the list above.
- Do the same with Starting_Scene.
UI Text in the Game Scene
- Add a text UI object, name it Restart_text.
- Text = Press the “R” key to restart the level
- Position it at 0x and 100y
- Anchor it to the bottom-middle.
- Font = 28
- Alignment = middle
- Horizontal and Vertical Overflow = Overflow
- And make the color white.
Restarting Scene Behavior
- Add a new empty game object, rename it Game_Manager.
- Make a new C# script, name it GameManager, and drag it onto the Game_Manager.
- Open the GameManager script and use the code below:
- Remember to add the SceneManagement library, SceneManager won’t work without it. Now let’s call the GameOver() method in the UIManager script!
- Update is saying that if you press R and if GameOver() method was called to make _isGameOver true, then load the Starting_Scene.
4. Head to the UIManager script.
5. Add variables and reference the GameObjects in the UIManager script component on Canvas.
6. Go down to the Start method and add the “GetComponent” statement:
7. Make a new method under the UpdateLives() method named “GameOverSequence()”.
8. Now, copy and paste everything that was in the if statement, “if(currentLives == 0)” into the new method, then call “GameOverSequence()” in the same if statement. Makes it neater and a lot easier to debug later if needed!
9. Now lets update the coroutine to make the Restart_text flicker too:
That is how to restart a Scene in unity! Next, let’s make a MainMenu screen!
Thank you for your time!