gpt4 book ai didi

c# - 通过 Unity 中的脚本更改作为 2D Sprite 子项的 TextMeshPro 文本

转载 作者:行者123 更新时间:2023-12-05 05:40:31 25 4
gpt4 key购买 nike

我有一个 2D 方形对象 Sprite ,它有一个存储数字的子 TextMeshPro 对象。

Unity 中的层次结构:

enter image description here

统一场景:

enter image description here

我想通过作为 2D 方形对象组件的脚本动态更改 TextMeshPro 对象中的数字。下面是我正在使用的代码。

'''

[SerializeField] private TextMeshProUGUI m_playerText;

// Start is called before the first frame update
void Start()
{
m_playerText.text = "8";
}

'''

但是我收到错误“NullReferenceException:对象引用未设置为对象的实例”

如果有任何反馈可以解决此问题,我们将不胜感激。

最佳答案

使用 GetComponentInChildren<TextMeshProUGUI>()解决问题:

void Start()
{
m_playerText = GetComponentInChildren<TextMeshProUGUI>();
m_playerText.text = "some text...";
}

关于c# - 通过 Unity 中的脚本更改作为 2D Sprite 子项的 TextMeshPro 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72401448/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com