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
EasyA 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 toscoreand shows it inscoreText, asScore: 6.LoseLife()takes 1 offlives(3 at the start) and shows it inlivesText, asLives: 2.- When
livesreaches 0 the game is over: showgameOverPaneland switch off the Player script so the arrows stop working. After that,AddScorechanges 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
}
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Building Real Games
2Buttons and Screens
Buttons and onClickLambdasWiring in the InspectorPanels On and OffHealth BarsRecap: Upgrade Shop5Saving with PlayerPrefs
Save a NumberLoad with a DefaultHigh ScoreSave SettingsRecap: Coins in the Bank3The Game Manager
One Script Runs the GameThe SingletonGame StatesSwitching on the StateRecap: Round Manager6Project: Endless Runner
Three LanesObstacles Ahead9The New Input System
Two Input SystemsKeyboard and MousePressed This FrameInput ActionsRecap: Input Swap