What are the Game Over and Win Screens?
I have played video games since my childhood, both indoors and outdoors. I find this activity pleasurable because it brings entertainment to other people who watch it. What I have found out is that most video games that I play, usually on PlayStation consoles, have their own over screen. I remember back in the day when I struggled playing Mega Man 8, where the level was pretty difficult to progress. I would always lose at that and needed several retries after the game over. There are also other games that are similar to arcade, action, and platformer games, like Street Fighter, which I used to play versus my younger brother at the time, who was good.
Recap On Previous Tutorial
In the previous tutorial, I mentioned starting and quitting the game by clicking the user interface. This happens during the gameplay of the application, where you start playing. In the tutorial, there is still no build yet, but I show in the editor where I click Start Game and bring the game into another scene. The gameplay scenes are actually what let you control the player movement to avoid the enemy and go to another position. All of that uses both input systems, which are the legacy and the new input system. Also, I have also made some collision systems work, especially between the enemy and player. Both actually use the physics system, which is known as Rigidbody2D.
Lost Game or Game Over User Interface
The more interesting fact about game development is how you will use the user interface to make the game over screen. This usually happens when the player has reached certain hit points and can no longer continue the game. Then they will go back to their previous value of hit points and the origin of their position. All of that happens during the gameplay system and the player interaction to continue or not. There are also arcade games that use this kind of system, but to continue or retry the game, the player must insert a coin. Those systems usually happen at places that host the game system for other players to enjoy game entertainment.
Adding The Game User Interface Panel
In the gameplay scene, which you usually use in the game project, find the Hierarchy tab, which displays all of the game objects in the scene. To add the panel, right-click in the Hierarchy tab and find User Interface UGUI with its sub-hierarchy of Panel. Unity will usually add the panel automatically to the existing Canvas. I actually used it to add a certain health slider to the game for the player, which already had a Canvas. So that Canvas is actually the parent of the other user interfaces that you use in the game, but it depends on their suitability. After you add the panel, resize it, which I set to a width of 500 and a height of 300.
Rect Transform Component on the Panel
Selecting the panel after you have created it will eventually bring up the interface section in the Inspector. See the Rect Transform component, where I have set the width and height to their respective values. I also set the anchor of the component to be in the middle centre. Now, inside the parent of the panel, add its child game objects for the game over screen. That will be a text game object that displays “Game Over” and a restart button. I forgot to mention that you can also add the panel by pressing the GameObject tab at the top and finding the Canvas UGUI. I renamed the panel to the Game Over Panel. Change the image component panel colour alpha to 1 instead of half, which is 0.5, so that it can be seen as white.
Adding Text Game Over to the Panel Parent
After you add the Text UGUI, set the anchor of that RectTransform GameObject to the top middle centre. That will position it at the top of the game over panel. Now rename the text to GameOverText, and inside the TextMeshProUGUI, input the text “Game Over.” Also, for the Y position value, set it to -50 so that it stays inside the panel. This will display “Game Over” on the game screen during the game. Also, add another button named RestartButton. This is a similar process to how to add the text: either go to the GameObject menu or add it directly in the Hierarchy. In the Hierarchy section, with the GameOverPanel selected, click the plus button below Hierarchy and find the Button UGUI.
Button Text and Copying Panel
After the button text has been added and renamed, set the value of its Pos Y to -100. Its anchor system works similarly to its GameOverText game object at the top middle. Now you will see that the button of the game object is below the GameOverText game object. For the button, you will also need to modify its TextMeshPro component, especially the text. Change the text to Restart in the component so that the button displays Restart in the game. After completing the layout for the GameOverPanel, select that panel in the hierarchy with a left-click, then right-click to find Duplicate. The duplicate will actually create two panels with their children also copied in the hierarchy.
Renaming Copy of Game Object
After I finish the GameOverPanel, I move on to the WinOverPanel, which I created above by copying the GameOverPanel GameObject. Now rename that copied GameObject to WinOverPanel. Its children behave similarly to the original GameObject you copied. I only need to rename the text GameOverText to WinOverText on the GameObject. I also need to change its component text from Game Over to You Win on the TextMeshPro UGUI component. Now the screen actually displays both GameOverPanel and WinOverPanel, and both appear visible. Turn off those two GameObjects in the Hierarchy so that only functions in the gameplay scripts turn them on.

Setting Up Code to Call Those Game Objects
Now, after you turn off those two GameObjects I created in the hierarchy, the next step is to add the GameObject variable in the script for reference. I decided to rename those GameObject variables with names that are almost similar to the hierarchy names. Those are gameOverPanel and winOverPanel for those public GameObject variables. Save and compile before you actually return to the Unity Editor. Not mentioned before, I actually added those two variables to the existing UIManager script, which is on the UIManager GameObject. Now you have the two variables for the two GameObjects, but you will need to drag those two GameObjects to the UIManager GameObject and script. Otherwise, it will return an error later during gameplay.

Finding The Line of Function Code to Call Game Over Panel
For the game over panel, I believe that it should be called when the player’s health reaches zero or is empty. Let’s open up Visual Studio by going to Assets on the top panel of the Unity Editor and opening the C# project in the sub-hierarchy. That will load the scripts so you can find the player’s health to call that GameObject. So, I found out that the PlayerHealth script is actually the one that needs to be configured with the UGUI GameOverPanel. In the Update function of the HealthPlayer script, there is a check for the player’s health. Instead of destroying the GameObject, call the UIManager singleton to open the game over panel. You can also make a new routine or function in the UIManager before calling it.
Making Function Game Over Panel
In the UI manager script, make a public method named SetGameOverPanel with a bool parameter named condition. This condition will actually be used to activate and deactivate the GameObject of the variable gameOverPanel. Inside this method, call gameOverPanel.SetActive(condition). This will be used based on the condition that is passed with the parameter in the PlayerHealth script. It is based on the condition of the parameter, which is either true or false. The GameOverPanel itself automatically deactivates during gameplay. Now I am near the conclusion, which is that you probably already learned how to add a GameObject, a panel, and TextMeshPro. Most of that is related to the UGUI, which is always used in Unity. I decided to continue in the next chapter of the tutorial. Let me know what you think in the comment section.
I drafted this article from my own perspective and used AI to polish the language.
Concept art (created with generative AI, touched up manually).
Add and fix words using Grammar.com


Leave a Reply