Script not reacting to when event is called!

I have a gameobject that subscribes to an event but when the event is fired the Method does not get executed. My question is does the gameobject needs to be active on gamestart to subscribe to events?

public StationData station;

    private void Awake()
    {
        GameManager.OnGamePhaseChanged += SetUIElementActive;
    }

    private void OnDestroy()
    {
        GameManager.OnGamePhaseChanged -= SetUIElementActive;
    }

    private void SetUIElementActive(GamePhase newPhase)
    {
        Debug.Log(newPhase.id == station.id);
        if (station.id == newPhase.id)
        {
            gameObject.SetActive(true);
        }
    }

    private void Start()
    {
        gameObject.SetActive(false);
    }

Yes, it’s a bit of an obscure idiosyncracy, but if a game object in a scene is not active, the Awake() method will not be called at all. That can definitely trip people up, but that’s the way it is. Keep it in mind, and don’t forget it. If you disable the StationData component, but the game object it’s attached to is active in the scene, you should still get the Awake() call.

1 Like