如何解决使用TextMeshPro的OnTriggerEnter2D
如果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变量)
否则,该错误通常只是意味着您忘记了在检查器中为该变量分配某些内容。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。