2D Galaxy Shooter — Pressing the R Key to Restart the Game After Death

Hello all!

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.

Starting scene then move to the Game scene

Creating a New Scene

  1. If you don’t already have a “Scenes” folder in your Assets folder, create one now.
  2. Create a new scene in the Scenes folder.
  3. Rename it “Starting_Scene”.
  4. Select the Camera and look at the Camera component.
  5. Change flags to Solid Color, and the Background to black.
  6. Get the picture you used for the background and drag it into the scene.
  7. Save the scene, then head back to the game scene.
  8. Select the Background there, and head to the Transform component.
  9. At the “3 dot” drop down menu, click “Copy component”.
  10. Head back to the Starting_Scene.
  11. 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!

  1. Make a Starting_Game_UI script and drag it onto Canvas.
  2. Make 2 new Text UI objects:
    Starting_Game_text
    Countdown_text
  3. 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

Build Settings Window
  1. While in the Game scene, click file, then “Build Settings…”.
  2. Now click the “Add Open Scenes” button, that will add the current scene to the list above.
  3. Do the same with Starting_Scene.

UI Text in the Game Scene

  1. Add a text UI object, name it Restart_text.
  2. Text = Press the “R” key to restart the level
  3. Position it at 0x and 100y
  4. Anchor it to the bottom-middle.
  5. Font = 28
  6. Alignment = middle
  7. Horizontal and Vertical Overflow = Overflow
  8. And make the color white.

Restarting Scene Behavior

  1. Add a new empty game object, rename it Game_Manager.
  2. Make a new C# script, name it GameManager, and drag it onto the Game_Manager.
  3. 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!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Marcus Cohee

Marcus Cohee

20 Followers

I am starting Unity and learning the ropes as I go! Let’s take this journey together!