我试图在死亡时创建一个弹出UI,但似乎无法使用“ FindObjectOfType”从游戏管理器触发我的DeathUI代码。

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




public class DeathUI : MonoBehaviour
{
    public static bool GameOver = false;

    public GameObject deathMenuUI;

    public void deathUI ()
    {
        Debug.Log("game over");
        deathMenuUI.SetActive(true);
        Time.timeScale = 0f;
        GameOver = true;
    }
}


我在Gamemanager中获得了Debug,但在DeathUI中却没有得到...谢谢!

最佳答案

FindObjectOfType将找不到禁用的对象。如Unity Docs here中所述


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


您需要启用DeathUi GameObject才能使用FindObjectOfType或在GameManager类中引用它:

[SerializeField]
private DeathUI deathUi;

// ...

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

09-27 13:13