问题描述
如果b1被触发,我正在尝试更改文本。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class Player_Start : MonoBehaviour
{
[SerializeField] public TextMeshProUGUI textTest;
public void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("b1"))
{
textTest.text = "Now let's move the right leg up!";
transform.GetChild(0).gameObject.SetActive(false);
transform.GetChild(1).gameObject.SetActive(true);
}
if (collision.gameObject.CompareTag("b2"))
{
transform.GetChild(1).gameObject.SetActive(false);
transform.GetChild(3).gameObject.SetActive(true);
}
}
}
我说错了
NullReferenceException:对象引用未设置为对象Player_Start.OnTriggerEnter2D的实例(UnityEngine.Collider2D碰撞)(在Assets / Player_Start.cs:16处)
第16行说:
textTest.text = "Now let's move the right leg up!";
解决方法
如果您进行更改,它是否起作用:
TextMeshProUGUI textTest
到
TMP_Text textTest
(然后将tmp游戏对象重新分配给脚本上的textTest变量)
否则,该错误通常只是意味着您忘记了在检查器中为该变量分配某些内容。