Unity Basics Course: Creating an Ending Screen – Victory

Introduction

Hello! In this course, we will learn how to create an ending screen in Unity for games. We will particularly focus on the ending screen that will be displayed when a victory condition is met. The ending screen plays a significant role in game development as it provides important feedback to the player and greatly influences the overall gaming experience. In this article, we will explain the components of the ending screen, UI design, script writing methods, and more in detail.

1. Understanding the Ending Screen

The ending screen is the screen displayed to the player at the last stage of the game. It appears when the victory conditions are satisfied, and typically consists of the following elements:

  • Victory message
  • Score, rank, and other statistical information
  • Restart button
  • Button to return to the main menu
  • Background image or animation

2. Setting Up the Unity Project

First, open the Unity project and create a new scene for the ending screen. Follow these steps:

  1. In the Unity Editor, click the File menu and select New Scene.
  2. Name the scene EndingScreen and save it.
  3. Right-click in the Hierarchy panel and choose UI -> Canvas to create a canvas.
  4. Add various UI elements under the canvas.

3. Configuring the UI

Now, let’s set up the necessary UI elements for the ending screen. The basic elements needed for the ending screen include:

3.1. Adding a Victory Message

Add text under the Canvas to display the victory message. To add text:

  1. Right-click on the Canvas in the Hierarchy and select UI -> Text.
  2. Modify the properties of the Text object to write the victory message. For example, you can use the message “Congratulations! You have won!”.

3.2. Displaying Scores and Statistics

To show the score and statistics obtained in the game, add another Text element.

  1. Right-click on the Canvas in the Hierarchy and select UI -> Text.
  2. Modify the properties of the Text object to dynamically update the score information by connecting a script.

3.3. Adding Buttons

Add buttons that allow the player to restart or return to the main menu. Here’s how to add buttons:

  1. Right-click on the Canvas in the Hierarchy and select UI -> Button.
  2. Replace the button text with phrases like “Restart” or “Main Menu”.
  3. Adjust the button size and position in the Inspector window to make it look good.

4. Adding Scripts

Now that the UI elements of the ending screen are ready, let’s write a script to display this screen when the victory condition occurs. You can use the following approach:

4.1. Creating the EndingScreenManager Script

Follow these steps in the Unity Editor to create the script:

  1. Create a Scripts folder in the Project view.
  2. Right-click in the Scripts folder, select Create -> C# Script, and name it EndingScreenManager.
  3. Double-click the script to open it in the code editor.

4.2. Writing the Script Code

Here is a basic code example for the EndingScreenManager script:


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

public class EndingScreenManager : MonoBehaviour
{
    public GameObject endingScreen;
    public Text scoreText;

    void Start()
    {
        endingScreen.SetActive(false);
    }

    public void ShowEndingScreen(int score)
    {
        endingScreen.SetActive(true);
        scoreText.text = "Your Score: " + score.ToString();
    }

    public void RestartGame()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }

    public void GoToMainMenu()
    {
        SceneManager.LoadScene("MainMenu");  // Change MainMenu to the actual scene name.
    }
}
    

4.3. Connecting UI Elements and the Script

Now that the script is ready, let’s connect the UI elements to the script:

  1. Select the Canvas in the Hierarchy panel, then add the EndingScreenManager script.
  2. Drag and connect the GameObject of the ending screen to the Ending Screen field of the EndingScreenManager component.
  3. Drag and connect the Text object that will display the score to the Score Text field.

5. Displaying the Ending Screen at Game End

Set up the game to call the ShowEndingScreen method to display the ending screen when the game ends. You can add the following code at the location where the victory condition is confirmed:


if (playerHasWon)
{
    endingScreenManager.ShowEndingScreen(playerScore);
}
    

6. Decorating the Ending Screen

The ending screen should also be visually appealing. Here are some tips for creating an attractive ending screen:

  • Use color combinations that match the background image.
  • Add animation effects to create excitement about the ending.
  • Adjust the font style of the text to enhance readability.

Conclusion

This tutorial guided you on how to create an ending screen in Unity. By showing the ending screen at the right moment when the victory condition is satisfied and providing a congratulatory message and statistical information to the player, you can offer a more satisfying gaming experience. Now, add a fantastic ending screen to your game to give players an unforgettable moment!

Appendix

If you have any additional information or questions, please leave a comment. If you want more Unity-related tutorials, please subscribe to the blog!