Unity-Start方法未运行



我是Unity的新手,我正在学习一个flappy bird教程,以更加熟悉游戏引擎。我正在学习CodeMonkey教程。我在屏幕上看比赛。这是我附加到GameOverWindow的脚本。但只有Awake((被调用。开局没有。正因为如此,我的活动无法运行,所以Game over窗口不会显示。

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);
}
}

根据Unity文档

Start在脚本的生命周期中只调用一次。但是,无论脚本是否启用,在初始化脚本对象时都会调用Awake。如果脚本在初始化时未启用,则不能在与Awake相同的帧上调用Start。

因此,如果从一开始就禁用GameOverWindow,则不会执行Start,但会执行Awake。您可以将事件初始化移动到Awake,它应该可以工作(只要添加事件有问题,而不是事件中的代码(。

最新更新