Build a 2D Platformer using Unity and C#

Lesson 6 of 7

UI: Score, Health, and Level Complete

Wire the systems from earlier lessons to a UI a player can actually see, using a small manager class that everything else talks to.

using UnityEngine;
using TMPro;

public class GameManager : MonoBehaviour
{
    public static GameManager Instance { get; private set; }

    public TMP_Text scoreText;
    public GameObject levelCompletePanel;

    int score;

    void Awake()
    {
        Instance = this;
    }

    public void AddScore(int amount)
    {
        score += amount;
        scoreText.text = $"Score: {score}";
    }

    public void CompleteLevel()
    {
        levelCompletePanel.SetActive(true);
        Time.timeScale = 0f;
    }
}

The pieces

  • public static GameManager Instance { get; private set; } is a lightweight singleton: any script can reach the one GameManager in the scene with GameManager.Instance, instead of every script needing a manually-dragged reference. That's how Coin called GameManager.Instance.AddScore(...) in the previous lesson.
  • scoreText.text = $"Score: {score}" is the same pattern from the Unity Essentials course, code pushes new values into the UI, the UI never updates itself.
  • levelCompletePanel is a Canvas child (a panel with a background and text) that starts inactive in the Inspector and gets SetActive(true) only when the level is finished.
  • Time.timeScale = 0f pauses all physics and any code using Time.deltaTime (like the patroller's movement), a simple way to freeze gameplay behind a completion or pause screen without disabling every script individually.

WARNING

Setting Time.timeScale = 0f also freezes Update()-based countdowns and animations driven by deltaTime. Resetting it back to 1f (e.g. when restarting the level) is easy to forget, and leaves the whole game frozen.

📝 UI & Game State Quiz

Passing score: 70%
  1. 1.What does the GameManager.Instance pattern let other scripts do?

  2. 2.Setting Time.____ to 0 pauses physics and any code driven by deltaTime, useful for a level-complete or pause screen.

  3. 3.UI text updates itself automatically whenever the underlying score variable changes.