Unity Basics Course: Creating an Ending Screen (Defeat)

In game development, the ending screen is an important element that wraps up the player experience. In this tutorial, we will explain how to create a simple ending screen in Unity, with a focus on implementing the ending screen in the event of a defeat condition.

1. Installing Unity and Setting Up the Project

After installing Unity, create a new 2D or 3D project. Once the basic structure of the game is set up, you need to create a dedicated scene for the ending screen. Here’s how to create a new scene:

  • Select File → New Scene.
  • Save the scene. For example, name it “EndingScreen”.

2. Adding Basic UI Elements

To create the ending screen, you need to add user interface (UI) elements. You can use Unity’s UI system to add buttons, text, and image elements. Follow these steps:

  • Right-click in the Hierarchy window and select UI → Canvas to create a new canvas.
  • Add UI → Panel inside the canvas to create a background.
  • Add UI → Text within the Panel to display the “Game Over” message.
  • Add UI → Button to create a “Restart” button.
  • Adjust the properties of the Text and Button to change their style as desired.

2.1 Adjusting Text Properties

Select the text element and enter an appropriate message in the Text property of the Inspector window. Modify font size, color, etc., to enhance readability.

2.2 Adjusting Button Properties

Set the button text to “Restart” and configure the button to call a specific method upon clicking. To do this, add the method to the On Click() event of the Button component.

3. Setting Defeat Conditions

You need to set defeat conditions in the game to create events that transition to the ending screen. For instance, you can configure it to transition to the ending screen when the player’s health reaches 0.

  • Implement health reduction logic in the Player script.
  • When health reaches 0, call SceneManager.LoadScene("EndingScreen"); to transition to the ending screen.

4. Implementing the Ending Screen Script

The ending screen script will be implemented to restart the game when the button is clicked. Below is an example of a script that manages the ending screen.

        
        using UnityEngine;
        using UnityEngine.SceneManagement;
        using UnityEngine.UI;

        public class EndingScreen : MonoBehaviour {
            // Method called when the restart button is clicked
            public void OnRestartButtonClicked() {
                // Restart the game from the first scene
                SceneManager.LoadScene("GameScene");
            }
        }
        
    

5. Connecting Ending Screen UI and Script

Now, you need to add the ending screen script to the ending screen canvas and connect the method to the button’s On Click() area.

  • Select the Panel or Canvas object.
  • In the Inspector window, click “Add Component” and add the “EndingScreen” script.
  • Select the Button object and drag the EndingScreen script into the On Click() event.
  • Select the “OnRestartButtonClicked()” method from the dropdown menu.

6. Final Testing and Improvements

Run the game to check if the ending screen works properly by inducing defeat conditions. Verify that the ending screen appears correctly and that it returns to the game scene when the button is clicked. If necessary, adjust the layout or style of the UI elements for improvements.

6.1 Implementing Additional Features

You can add various features to the ending screen besides statistics, scores, or the restart button. For example, consider adding:

  • Game timer display
  • High score tracking
  • Return to main menu button

Conclusion

In this tutorial, we learned how to create an ending screen in Unity upon defeat. The ending screen leaves an important impression on players and serves to provide feedback on game play. We hope you can enhance the player experience by adding an ending screen to your game.