统一文本出现问题,NullReferenceException



我正在尝试以统一方式显示计时器。我总是得到这样的错误:NullReferenceException:对象引用没有设置为对象的实例。如果有人能帮我,那就太好了<3

public class PlayerScore : MonoBehaviour
{
private float timeLeft = 120f;
public float playerScore=0;
public GameObject timeLeftUI;
public GameObject playerScoreUI;
private void Start()
{

timeLeftUI.gameObject.GetComponent<Text>().text = ("Time: " + timeLeft).ToString();
}
// Update is called once per frame
void Update()
{
timeLeft -= Time.deltaTime;


timeLeftUI.gameObject.GetComponent<Text>().text = ("Time: " + timeLeft).ToString();

//Debug.Log(timeLeft);
if (timeLeft < 0.1) //si le time est plus petit que 0 alors la partie se termine
{
//partie terminé
}
}
void CountScore()
{
playerScore = playerScore + (timeLeft * 10);
}
}```

尝试将脚本附加到GameObject的适当层次结构中,您可以将其从Unity中的资产文件夹直接拖动到GameObject。

由于尝试使用引用而该引用未初始化,因此引发错误NullReferenceException: Object

最新更新