My journey becoming a Unity game developer: Loading Scenes in Unity

Rhett Haynes
4 min readAug 20, 2022

--

Objective: When the player loses all their lives, not only will the Player see the Game Over text onscreen, but they will see a message telling them to press the “R” key to restart the level. After the player presses the “R” key, the game will restart as a new game to be played.

Restart Level text plays with Game Over, and game restarts when “R” key is pressed.

Start by creating an UI->Text-TextMeshPro name Restart_Level_text. Type in the Text field the message of “press the “R” key to restart the level”, and position the text in the middle of the screen and field box.

Restart Level text object created with message to restart level.

Next thing that needs to be done is to create an Empty game object name Game Manager to handle if the Player wants to restart the level and play again, or quit the game. Also, create a new C# script name GameManager, and attach it to the Game Manager object.

Inside the GameManager script, create a new variable Type bool name _isGameOver. This variable will be used as a Setter to say if the game is over or not. If you want to see the _isGameOver variable in the Inspector for test purposes, use the SerializeField attribute.

Next, create a new method that’s Public with a Type Void name GameOver(), and set the _isGameOver variable to True. This method will be used in the UI Manager script to indicate that when the Player has no lives left, the game is over and to run the game over sequence.

Since we’re not using the Start() function, just delete it so it doesn’t interfere with our code’s order of execution. Inside the Update() function, check IF the “R” key has been pressed by using the Input.GetKeyDown(KeyCode.R), and IF the game is over. If so, we want to play this scene again, but before we can activate this scene we must use the SceneManagement namespace by using Unity Engine. After that is done, go back into the IF condition’s code block and use the SceneManager to Load the Scene of index zero which will be the Game’s scene.

Game Manager script created to reload scene if game is over.

Over in the UI Manager script, we need to create 2 variables. The 1st variable will be to gain access to the Restart_Level_text game object using a SerializeField Type TMPro.TMP_Text name _restartLevelText. The 2nd variable will access the Game Manager object by having a Type GameManager name _gameManager.

Inside Start(), we want the Restart_Level_text object to be turned off when the game starts. Therefore, use the _restartLevelText variable to get the game object, and SetActive() it to False. With the _gameManager, we must find the Game Object by Finding Object Of Type <GameManager>. Then, we must Get the Component of the GameManager script. Make sure to NULL check IF the _gameManager’s component is active, and if not to leave a LOGERROR message saying the “Game Manager is NULL.

Access handles created for the Game Manager and Restart Level text objects.

Down in the UpdateLives() method, we want to use the GameOver() method to set the game over to true after the Player has zero lives remaining. Also, we want to turn on the Game Over text and Restart Level text, plus use the coroutine to flicker the Game Over text off and on. To organize all this, let’s create a new method Type Void name GameOverSequence().

Inside GameOverSequence() method, we will start by indicating that the game is over using _gameManager’s GameOver() method. Next, take the _gameOverText and _restartLevelText game objects and SetActive() them to True. Finally, use the StartCoroutine() GameOverFlickerRoutine() coroutine to animate the “GAME OVER” text every 0.5 seconds.

Game Over Sequence method created to play when the player has no more lives.

In the Hierarchy, select the Canvas and drag the Restart_Level_text object into its slot on the UI Manager’s script component. Now go to File->Build Settings, and click on the Add Open Scenes button, or drag the Game scene onto the Scenes in Build list. This will add the scene we’re currently onto the Scene’s list with an Index number of (0) which represents the 1st scene in our game. Just click on the close button without building the game yet.

Set Restart Level text to UI Manager and added Game scene to Build Settings.

When we play the game and lose all of our lives, the text appears onscreen. Then, when we press the “R” key, the game’s level restarts from the beginning for us to play again.

Restart Level text plays with Game Over, and game restarts when “R” key is pressed.

--

--

Rhett Haynes
Rhett Haynes

Written by Rhett Haynes

Learning to become a Unity game developer.

No responses yet