0

I'm new to Unity and i am following a flappy bird tutorial to get a little more familiar with the game engine. i'm following the CodeMonkey tutorial. i'm at the game over screen. this is the script i have attached to my GameOverWindow. but only the Awake() gets called. the start doesn't. because of this my event is not working so the Game over window does not show.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using CodeMonkey.Utils;

public class GameOverWindow : MonoBehaviour
{
    private Text scoreText;
    // Start is called before the first frame update
    private void Start()
    {
        Bird.GetInstance().OnDied += Bird_OnDied;
    }

    private void Awake()
    {
        scoreText = transform.Find("scoreText").GetComponent<Text>();

        transform.Find("retryBtn").GetComponent<Button_UI>().ClickFunc = () => { UnityEngine.SceneManagement.SceneManager.LoadScene("GameScene"); };
        Hide();
    }

    private void Bird_OnDied(object sender, System.EventArgs e)
    {
        scoreText.text = Level.GetInstance().GetPipesPassedCount().ToString();
        Show();
    }

    // Update is called once per frame
   private void Update()
    {

    }

    private void Hide()
    {
        gameObject.SetActive(false);
    }
    private void Show()
    {
        gameObject.SetActive(true);
    }
}
Bjorn Fontaine
  • 426
  • 1
  • 4
  • 17

1 Answers1

4

According to Unity docs

Start is called exactly once in the lifetime of the script. However, Awake is called when the script object is initialised, regardless of whether or not the script is enabled. Start may not be called on the same frame as Awake if the script is not enabled at initialisation time.

So if GameOverWindow is disabled from the beginning, Start will not be executed, but Awake will. You can move your event initialization to Awake and it should work (as long as it's a problem with adding event, not code in event).

flabbet
  • 327
  • 3
  • 14