所以,我是 Unity 的新手,我正在尝试遵循教程,在教程中,他们试图获取输入字段的文本,但是当我尝试它时,我收到错误:
'GameObject' does not contain a definition for 'text' and no accessible extension method 'text' accepting a first argument of type 'GameObject' could be found (are you missing a using directive or an assembly reference?)
我的代码如下所示:
[SerializeField] private GameObject UsernameInput;
...
Debug.Log(UsernameInput.text);
我已经添加了我的UsernameInput
,如下所示:
MyImage
我看过this但这没有帮助。
任何帮助将不胜感激。
最佳答案
游戏对象不包含名为“text”的字段。您要访问的位置是 InputField 类型的组件的字段“text”。
因此,在您的代码中,您应该按如下方式调用它:
Debug.Log(UsernameInput.GetComponent<InputField>().text);
我让你here有关 Unity 中组件的文档的超链接,其中介绍了组件的含义。
关于unity-game-engine - 团结 : get text from Input field,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68296264/