为什么在尝试更改 TextObject 的文本时出现错误

问题描述

我的脚本中有一个 TextObject,它附加到我的画布上,如屏幕截图所示。每当我尝试运行它时,我都会遇到该错误,我尝试了很多方法,但似乎无法弄清楚为什么会出现此错误

using System;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;


//Todo: Implement Death screen when health falls to 0 - respawn priority(high)
public class DeathScreen:MonoBehavIoUr
{

    public Text timeLived;
    public Button replay;
    public Button mainMenu;
    public Button retry;
    

    // ReSharper disable Unity.PerformanceAnalysis
    public void Setup(int timeLivedInSeconds)
    {
        //SceneManager.UnloadSceneAsync("scenes/main");
        SceneManager.LoadScene("Scenes/deathScene");
        timeLived.text = "Time Survived: " + timeLivedInSeconds;
        retry.onClick.AddListener(Retry);
        replay.onClick.AddListener(Replay);
        mainMenu.onClick.AddListener(MainMenu);
    }

    
    public void Replay()
    {
        //Todo: call Replay
    }

    public void Retry()
    {
        SceneManager.UnloadSceneAsync("scenes/death");
        SceneManager.LoadScene("scenes/main");
    }

    public void MainMenu()
    {
        SceneManager.UnloadSceneAsync("scenes/death");
        SceneManager.LoadScene("scenes/menu");
    }
}

enter image description here

enter image description here

错误

NullReferenceException: Object reference not set to an instance of an object
DeathScreen.Setup (system.int32 timeLivedInSeconds) (at Assets/Scripts/Mechanics/DeathScreen.cs:23)
DeathScreen.Start () (at Assets/Scripts/Mechanics/DeathScreen.cs:16)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)