Menu
CoddyTech

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 icon

Challenge

Easy

Finish the coin grab; SetState is written.

  1. Add public static RoundManager Instance;, set in Awake().
  2. In Update(), switch on state: Ready waits for Space; Playing counts timeLeft down, then goes to the next Ready, or Finished after the last round.
  3. public bool TakeCoin(): only while Playing, count it, show Coins: 2, return true. Coin.cs destroys 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
}
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

Practice on your own: Unity playground