unity-game-engine - 团结 : get text from Input field

标签 unity-game-engine

所以,我是 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/

相关文章:

c# - 在Unity3d中使用MySQL,填充数据表

c# - 无法通过 Unity 将数据写入 Firebase 数据库

c# - Unity手机SSL连接错误

unity-game-engine - 如何在 UI 中设置相机 View 特定区域

ubuntu - 从 Ubuntu 构建 Unity3D

c# - Unity3D优化代码

unity-game-engine - 无法部署到 HoloLens(错误 80004005)

c# - Unity事件系统如何知道我是否实现了IDragHandler

performance - Unity3D 中的rigidbody.velocity 与 lerp 效率

json - Unity 反序列化为可编写脚本的对象