C# FindObjectOfType问题

C# FindObjectOfType问题,c#,unity3d,C#,Unity3d,我试图在死亡时创建一个弹出式UI,但我似乎在使用“FindObjectOfType”从游戏经理那里触发我的死亡代码时遇到了问题 public class GameManager : MonoBehaviour { public float slowness = 10f; public void Start() { PlayerPrefs.SetInt("Score", 0); } public void EndGame() {

我试图在死亡时创建一个弹出式UI,但我似乎在使用“FindObjectOfType”从游戏经理那里触发我的死亡代码时遇到了问题

public class GameManager : MonoBehaviour
{
    public float slowness = 10f;

    public void Start()
    {
        PlayerPrefs.SetInt("Score", 0);
    }

    public void EndGame()
    {
        StartCoroutine(DeathScreen());
    }

    IEnumerator DeathScreen()
    {
        Time.timeScale = 1f / slowness;
        Time.fixedDeltaTime = Time.fixedDeltaTime / slowness;

        yield return new WaitForSeconds(1f / slowness);

        Time.timeScale = 1f;
        Time.fixedDeltaTime = Time.fixedDeltaTime * slowness;

        //SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
        Debug.Log("death");
        FindObjectOfType<DeathUI>().deathUI();
    }
}

我在Gamemanager中得到调试,但在DeathUI中没有。。。谢谢大家!

FindObjectOfType将找不到禁用的对象。如联合文件所述

返回类型为的第一个活动加载对象

您需要启用DeathUi游戏对象才能使用FindObjectOfType或在GameManager类中引用它:

[SerializeField]
private DeathUI deathUi;

// ...

Debug.Log("death");
deathUi.deathUI();

你有错误吗?例如,
NullReferenceException
?没有错误,我只是无法让deathUI正常工作:(啊,我明白了!谢谢。我将代码完全移动到了我的游戏管理器中,因此我不必使用FindObjectOfType!)
[SerializeField]
private DeathUI deathUi;

// ...

Debug.Log("death");
deathUi.deathUI();