Menu
CoddyTech

One Script Runs the Game

Part of the Building Real Games section of Coddy's Unity journey. Lesson 12 of 56.

Put the score in the player's script, the lives in a health script and the game-over check in a third, and every script needs the others' numbers, so each new rule touches several files. Unity games usually give those numbers to one script, the GameManager, on an empty GameObject of its own. It owns the score, the lives and the rule that ends the game. Other scripts only report what happened, by calling its public methods. In this penalty shootout the net reports each goal:

public class GameManager : MonoBehaviour
{
    private int goals;
    private int shotsLeft = 5;

    public void ShotTaken(bool scored)
    {
        if (shotsLeft == 0) return;
        shotsLeft--;
        if (scored) goals++;
        Debug.Log("Goals: " + goals + ", shots left: " + shotsLeft);
        if (shotsLeft == 0) Debug.Log("Full time: " + goals + " goals");
    }
}
// Net.cs, on the goal's trigger
public GameManager game;

void OnTriggerEnter(Collider other)
{
    if (other.CompareTag("Ball")) game.ShotTaken(true);
}

The net does not know how many shots are left or when the game ends. It reports a goal, and the manager decides what that means; the keeper's script reports a save with game.ShotTaken(false). For a shootout of 3 shots you change one number in one script. The check at the top of ShotTaken stops a ball that rolls in after full time from counting.

You fill the game field in the Inspector by dragging the GameManager object onto it, and Unity finds the GameManager script on that object. The manager is also the place for the HUD: when it holds the score text and the game-over panel, the screen changes in the same method that changes the numbers, and no other script touches the UI.

challenge icon

Challenge

Easy

A gem hunt. The arrows walk the Player (its script is ready). Gems are worth their points, bombs cost a life, and a GameManager keeps it all. The Inspector already links each gem's and bomb's game field to the GameManager object, and the GameManager's fields to the HUD and the Player.

In GameManager.cs:

  • AddScore(int points) adds the points to score and shows it in scoreText, as Score: 6.
  • LoseLife() takes 1 off lives (3 at the start) and shows it in livesText, as Lives: 2.
  • When lives reaches 0 the game is over: show gameOverPanel and switch off the Player script so the arrows stop working. After that, AddScore changes nothing.

In Gem.cs and Bomb.cs: when the Player (tagged Player) touches one, it reports to the game (a gem its points, a bomb a lost life) and removes itself.

Press Play, collect the gems and hit the bombs, then Check.

Try it yourself

using UnityEngine;
using TMPro;

public class GameManager : MonoBehaviour
{
    public TMP_Text scoreText;
    public TMP_Text livesText;
    public GameObject gameOverPanel;
    public Player player;
    private int score;
    private int lives = 3;
    private bool gameOver;

    public void AddScore(int points)
    {
        // add the points and show the new score, unless the game is over
    }

    public void LoseLife()
    {
        // take one life and show it; at 0 lives the game is over
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Building Real Games