【发布时间】:2022-07-27 23:13:20
【问题描述】:
它应该如何工作 - 当我点击 UI 按钮时,分数会增加并使用文本显示。
它是如何工作的 - 一个错误提示“NullReferenceException: Object reference not set to an instance of an object”
两个不同的游戏对象上有两个脚本。
播放器脚本
using UnityEngine;
public class Player : MonoBehaviour
{
ScoreManager scoreManager;
private void Start()
{
scoreManager = new ScoreManager();
}
public void UpdateScore()
{
scoreManager.IncrementScore();
}
}
ScoreManager 脚本
using UnityEngine;
using TMPro;
public class ScoreManager : MonoBehaviour
{
private int score = 0;
public TextMeshProUGUI scoreText;
public void IncrementScore()
{
score++;
scoreText.text = score.ToString();
}
}
当我使用 Debug.Log(score.ToString()) 时,它会在控制台中显示分数。但是当我使用 textmeshprougui 时,它会报错。
另外,我已将文本拖到检查器中,因此对于空引用来说这不是问题。我已经检查过很多次了。
为什么我无法从另一个脚本更新文本?
【问题讨论】: