Recap: Round Manager
Part of the Building Real Games section of Coddy's Unity journey. Lesson 16 of 60.
This chapter gave the game one manager: a singleton that others reach without a field, and states it switches between. The round manager puts both to work.
Challenge
EasyFinish the coin grab; SetState is written.
- Add
public static RoundManager Instance;, set inAwake(). - In
Update(), switch onstate: Ready waits for Space; Playing countstimeLeftdown, then goes to the next Ready, or Finished after the last round. public bool TakeCoin(): only while Playing, count it, showCoins: 2, return true.Coin.csdestroys a counted coin the Player touches.
Press Play all three rounds. Then Check.
Try it yourself
using UnityEngine;
using TMPro;
public enum RoundState { Ready, Playing, Finished }
public class RoundManager : MonoBehaviour
{
// the singleton: a static Instance, set in Awake()
public GameObject readyPanel;
public GameObject playPanel;
public GameObject finishedPanel;
public TMP_Text roundText;
public TMP_Text coinsText;
public TMP_Text finalText;
public int rounds = 3;
public float roundTime = 4f;
private RoundState state;
private int round = 1;
private int coins;
private float timeLeft;
void Start()
{
SetState(RoundState.Ready);
}
void Update()
{
// switch on state: Ready waits for Space; Playing counts the time down
}
void SetState(RoundState next)
{
state = next;
readyPanel.SetActive(next == RoundState.Ready);
playPanel.SetActive(next == RoundState.Playing);
finishedPanel.SetActive(next == RoundState.Finished);
if (next == RoundState.Ready) roundText.text = "Round " + round;
if (next == RoundState.Playing) timeLeft = roundTime;
if (next == RoundState.Finished) finalText.text = "Coins: " + coins;
}
// TakeCoin(): a coin counts only while Playing
}
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 SwapPractice on your own: Unity playground