微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决为什么在尝试更改 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 举报,一经查实,本站将立刻删除。