【问题标题】:NullReferenceException in UI Text component [duplicate]UI 文本组件中的 NullReferenceException [重复]
【发布时间】:2016-12-18 08:29:13
【问题描述】:

每当我在游戏结束后点击菜单按钮时,我都会收到这个NullReferenceException。现在由于这个错误,我的游戏挂了,我无法再次重玩。所以我必须重新启动它:

这是错误:

NullReferenceException: Object reference not set to an instance of an object
UiManager.Update () (at Assets/UiManager.cs:21)

这是代码:

public class UiManager : MonoBehaviour {
    float score=0;
    public Text scoreText;
    public Text endText;
    public Button[] buttons;
    public Text TIMER; 
    float time=30f;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        time -=Time.deltaTime;
        TIMER.text = "TIME: " + (int)time;
        if (time < 1) {
            GameOverF ();
            Time.timeScale = 0;
        }
    }

    public void IncrementScore(){

        score++;
        scoreText.text = "SCORE: " + score;
    }

    public void IncrementBadScore(){
        score-=11;
        scoreText.text = "SCORE: " + score;
    }
    public void IncrementGoodScore(){
        score+=11;
        scoreText.text = "SCORE: " + score;
    }


    public void IncrementRedScore(){

            score-=2;
            scoreText.text = "SCORE: " + score;
            }

    public void GameOverF(){
        endText.gameObject.SetActive (true);
        foreach (Button button in buttons) {
            button.gameObject.SetActive (true);

        }


    }

    public void exit(){
        Application.Quit ();
    }

    public void Play(){
        Application.LoadLevel ("level1");
    }
    public void Menu(){

        Application.LoadLevel ("menu");
    }


}

这是错误所在的行:

这是我的游戏对象的检查员:

一切都很好,但这个错误即将到来,我无法找出原因? 请帮忙!

【问题讨论】:

    标签: c# unity5 unity3d-2dtools


    【解决方案1】:

    Menu()Time.timeScale=1;

    就是这样

    实际上挂起问题是因为当游戏结束或计时器关闭时,我将Time.timeScale设置为0。所以下次我重新启动它时,它已经暂停了。

    挂起问题不是由于NullReferenceException 当你改变场景时文本组件被破坏并且在调用时导致此异常 !

    【讨论】:

    • 我请求您围绕您共享的代码添加一些上下文/评论。它将帮助提问者和其他读者更好地理解您的帖子。
    猜你喜欢
    • 1970-01-01
    • 2021-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多