After learning about the static events, I was curious if I could develop the TurnSystem and TurnSystemUI code without the singleton. It turns (no pun intended) out that it works. My revised TurnSystem class below. The caveat is that you must add the end turn button’s onClick listener through the inspector and not on the TurnSystemUI’s Start() method.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TurnSystem : MonoBehaviour
{
private static int turnNumber = 1;
public static event EventHandler OnTurnChanged;
public void NextTurn()
{
turnNumber++;
OnTurnChanged?.Invoke(this, EventArgs.Empty);
}
public static int GetCurrentTurnNumber()
{
return turnNumber;
}
}
Edit: I changed the name of the event to be OnTurnChanged just like Hugo has in the course. I found OnTurnChanged to be semantically more correct than what I had put (OnNextTurn). In the later lecture I found it very helpful to have that sematic cue that the turn already changed rather than a next turn request getting triggered but not necessarily executing.