Object Reference Not Set To Instance Of Object When Using InputField Script

huangapple go评论71阅读模式
英文:

Object Reference Not Set To Instance Of Object When Using InputField Script

问题

我刚刚开始学习如何在Unity中执行基本操作。我正在尝试学习如何使用文本字段框。我已经跟随了几个教程,并复制了所有内容,但我总是遇到以下两个问题之一。首先,我收到一个错误,指出对象引用未设置为对象的实例。或者,我无法在脚本检查器中分配文本字段对象。在设置我的项目时,我执行了以下操作:

  1. 创建了一个新画布(Canvas)。

  2. 添加了一个 InputField 对象。

  3. 添加了一个 Button

  4. 创建了一个新脚本,其中包含以下代码(这是我现在的代码,但我尝试了许多变种,包括使用 [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;
        }
    }
    
  5. 创建了一个空的 GameObject

  6. 将脚本附加到该 GameObject 上。

  7. 编辑按钮以调用 GetInput 方法。

  8. GameObject 检查器窗口中的 Input Field 组件中添加了 InputField 对象。我还尝试将来自 InputFieldText 对象添加到其中(不确定我是否对所有术语使用正确)。

请注意,我在检查器的右侧尝试过 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:

  1. Created a new canvas.

  2. Added an InputField object.

  3. Added a Button.

  4. 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 the GameObject to 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&lt;Text&gt;().text;
        }
    }
    
  5. Created empty GameObject.

  6. Attached the script to the GameObject.

  7. Edited the button to call the GetInput method.

  8. Added the InputField object to the Input Field component in the GameObject inspector window. I have also tried adding the Text object from the InputField (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&lt;Text&gt;().text;
    }

to:

public void GetInput (){
        msg = inputField.GetComponent&lt;TMP_InputField&gt;().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&lt;TextMeshProUGUI&gt;().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.

huangapple
  • 本文由 发表于 2023年3月1日 09:11:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75598753.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定