You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity中使用Text Mesh Pro动态生成输入框文本不显示问题咨询

问题原因与解决方案

核心错误

你当前的代码错误地尝试从输入框的根GameObject上获取TextMeshProUGUI组件:TMP输入框的根对象挂载的是TMP_InputField组件,文本显示组件是输入框的子对象,直接从根对象获取会返回空,自然无法设置文本。

修复步骤

  • 优先使用已经生成的newinput引用操作,不要使用GameObject.Find,避免重名匹配错误、性能损耗问题
  • 直接获取TMP_InputField组件设置text属性即可,不需要手动查找子对象的文本组件
  • 额外检查:如果修改后还是不显示,确认文本颜色和背景色没有重合、TMP字体图集包含你要显示的字符、输入框的RectTransform没有超出父Canvas显示范围

修正后代码

using TMPro; // 注意提前引入TMP命名空间
public GameObject inputs;
public GameObject beginplek;

// Start函数会在第一帧更新前调用
public void Start()
{
    GameObject newinput = Instantiate(inputs, beginplek.transform.localPosition, beginplek.transform.localRotation) as GameObject;
    newinput.transform.SetParent(GameObject.FindGameObjectWithTag("aaaa").transform, false);
    newinput.name = "periode12";
    // 直接获取输入框组件设置文本即可
    newinput.GetComponent<TMP_InputField>().text = "Hello World";
}

如果确实需要直接操作文本显示组件,也可以替换为:

newinput.GetComponentInChildren<TextMeshProUGUI>().text = "Hello World";

内容的提问来源于stack exchange,提问作者zackrn23

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 01:57:01