英文:
Object Reference Not Set To Instance Of Object When Using InputField Script
问题
我刚刚开始学习如何在Unity中执行基本操作。我正在尝试学习如何使用文本字段框。我已经跟随了几个教程,并复制了所有内容,但我总是遇到以下两个问题之一。首先,我收到一个错误,指出对象引用未设置为对象的实例。或者,我无法在脚本检查器中分配文本字段对象。在设置我的项目时,我执行了以下操作:
-
创建了一个新画布(Canvas)。
-
添加了一个
InputField
对象。 -
添加了一个
Button
。 -
创建了一个新脚本,其中包含以下代码(这是我现在的代码,但我尝试了许多变种,包括使用
[SerializeField]
或将GameObject
更改为InputField
):using UnityEngine; using System.Collections; using UnityEngine.UI; public class UserInput : MonoBehaviour { public GameObject inputField; public string msg; public void GetInput () { msg = inputField.GetComponent<Text>().text; } }
-
创建了一个空的
GameObject
。 -
将脚本附加到该
GameObject
上。 -
编辑按钮以调用
GetInput
方法。 -
在
GameObject
检查器窗口中的Input Field
组件中添加了InputField
对象。我还尝试将来自InputField
的Text
对象添加到其中(不确定我是否对所有术语使用正确)。
请注意,我在检查器的右侧尝试过 InputField 和 Text
调用 GetInput 方法。
英文:
I just started learning how to do basic things in Unity. I am trying to learn how to use a text field box. I have followed several tutorials and copied everything but I always end up running into one of two problems. First I get the error stating that the object reference is not set to an instance of an object. Or second, I am unable to assign a text field object in the script inspector. When setting up my project I did the following:
-
Created a new canvas.
-
Added an
InputField
object. -
Added a
Button.
-
Created a new script with the following code (this is what I have no but I have tried many variations including using the
[SerializeField]
or changing theGameObject
toInputField
):using UnityEngine; using System.Collections; using UnityEngine.UI; public class UserInput : MonoBehaviour { public GameObject inputField; public string msg; public void GetInput (){ msg = inputField.GetComponent<Text>().text; } }
-
Created empty
GameObject.
-
Attached the script to the
GameObject.
-
Edited the button to call the
GetInput
method. -
Added the
InputField
object to theInput Field
component in theGameObject
inspector window. I have also tried adding theText object
from theInputField
(not sure if I am using the correct terminology for everything)
Note on the right hand side in the inspector I have tried both InputField and Text
Call the GetInput method
答案1
得分: 1
以下是翻译好的部分:
"The Component you're trying to access on the 'InputField (TMP)' GameObject is actually a TMP_InputField
Class, not Text
, so just change:
public void GetInput () {
msg = inputField.GetComponent<TMP_InputField>().text;
}
to:
public void GetInput () {
msg = inputField.GetComponent<TMP_InputField>().text;
}
Just tested with the same hierarchy and that solves the issue apparently, msg's value becomes equal to the value of .text.
Just in case, if you attempt to do the same from the GameObject 'Placeholder' or 'Text', notice that they use a different Component, 'TextMeshPro - Text (UI)', in such case you'd use:
public void GetInput () {
msg = inputField.GetComponent<TextMeshProUGUI>().text;
}
Always pay attention to the attached components, there's a '(?)' symbol next to their names in Unity, that will take you to the corresponding page within Unity3D Docs."
英文:
The Component you're trying to access on the 'InputField (TMP)' GameObject is actually a TMP_InputField
Class, not Text
, so just change:
public void GetInput (){
msg = inputField.GetComponent<Text>().text;
}
to:
public void GetInput (){
msg = inputField.GetComponent<TMP_InputField>().text;
}
Just tested with the same hierarchy and that solves the issue apparently, msg's value becomes equal to the value of .text .
Just in case, if you attempt to do the same from the GameObject 'Placeholder' or 'Text', notice that they use a different Component, 'TextMeshPro - Text (UI)', in such case you'd use:
public void GetInput (){
msg = inputField.GetComponent<TextMeshProUGUI>().text;
}
Always pay attention to the attached components, there's a "(?)" symbol next to their names in Unity, that will take you to the corresponding page within Unity3D Docs.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论