我不断得到一个脚本上的错误,我正在编程的脚本工作,但我想修复错误
本文关键字:错误 脚本 编程 工作 一个 | 更新日期: 2023-09-27 18:17:31
我一直在分数计数器脚本上得到这个错误,我正在制作分数计数器工作,但这个错误只是令人讨厌,我不想发布它与错误堆积。
错误:NullReferenceException: Object reference not set to an instance of an object
ScoreCounter.Update () (at Assets/ScoreCounter.cs:26)
脚本:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ScoreCounter : MonoBehaviour
{
public static int score; // The player's score.
public Text text; // Reference to the Text component.
void Start ()
{
// Set up the reference.
//text = GetComponent <Text> ();
// Reset the score.
score = 0;
}
void Update ()
{
// Set the displayed text to be the word "Score" followed by the score value.
text.text = "Score:" + score;
}
}
从错误消息中可以清楚地看出,您的text
对象从未实例化过。
我不太了解你的用例,无法推荐它应该在哪里实例化,但在Start()内部看起来是个好地方。
欢呼