2

I am new to unity and want to get a value from a input text field. I found this question Get text from Input field in Unity3D with C#, but when I execute it the same error always appear: NullReferenceExcpetion : Object reference not set to an instance of an object. It seems like a stupid mistake and I tried everything but can't seem to fix it. My code:

void Start () {
    var input = gameObject.GetComponent<InputField>();
    input.onEndEdit.AddListener(SubmitName);

}

private void SubmitName(string arg0)
{
    Debug.Log(arg0);
}

I tried putting InputField input; before the start function and erasing var but still no luck. If anyone can help me with this problem it would be much appreciated. Pictures of where my scripts are attached at the moment. enter image description here

enter image description here

Community
  • 1
  • 1
Hendrien
  • 325
  • 1
  • 10
  • 20
  • you should include the script only on the canvas OR on the InputField. I would add it just to the canvas, so you have an element which will take control of his children (InputField and other UI elements). But please drag the `InputField` from the `Hierarchy` to the empty field at the bottom of the inspector where you can read **None (Input Field)** – Alex Cio Jul 23 '15 at 09:35
  • Oky thank you so much the input field could not be dragged but I created a new one and everything is in order. Thanks alot. – Hendrien Jul 23 '15 at 09:46
  • I just have one question the output is InputField(UnityEngine.UI.InputField) and not what I entered in the inputfield. – Hendrien Jul 23 '15 at 09:53
  • I really don't know what exactly is inside your script because you still didn't post it. But looks to me like you forgot to import the library for the ui `using UnityEngine.UI;` in the header so you can access the ui elements just by their name – Alex Cio Jul 23 '15 at 10:36

2 Answers2

0

Your code is nearly correct, if you have a look at the documentation, you have to call your method with function before calling the method name. It looks to me like you are mixing c# and JS, here is the `js function:

public class Example {
    public var mainInputField;
    public function Start() {
        // Adds a listener to the main input field 
        // and invokes a method when the value changes.
        mainInputField.onValueChange.AddListener(function() {
            ValueChangeCheck();
        });
    }
    // Invoked when the value of the text field changes.
    public function ValueChangeCheck() {
        Debug.Log("Value Changed");
    }
}

the c# solution:

public class MenuController : MonoBehaviour {

    [SerializeField]
    InputField inputText;

    // Use this for initialization
    void Start () {
        inputText.onValueChange.AddListener(delegate {
            DebugInput();
        });
    }

    private void DebugInput(){
        Debug.Log ("Input: " + inputText);
    }
}

My Hierarchie looks like this:

I created a Canvas and inserted an InputField inside it. The script with the code inside is attached to the Canvas and the InputField connected to the [SerializeField] inside the script in the Canvas.


I would recommend you to make your InputField a class variable, so you can access it easier later. Furthermore you should just create a [SerializeField] for your InputField so you can drag it to your script. This might avoid some mistakes too.

Alex Cio
  • 6,014
  • 5
  • 44
  • 74
  • Hi thanks for the reply but I still get the same error of the Object reference not set to an instance of an object. I added my script to the InputField. – Hendrien Jul 23 '15 at 08:57
  • Have you added the `InputField` inside your canvas and did you connect it with your script by dragging the `InputField` into the scripts placeholder inside the `inspector`? – Alex Cio Jul 23 '15 at 09:01
  • Yes I did both and my script is attached to the text of the InputField. – Hendrien Jul 23 '15 at 09:11
  • I just tried to but the script only on the InputField but it still gave the same error. – Hendrien Jul 23 '15 at 09:13
  • I explained it in my post how I setup everything. If this won't work, could you please make a screenshot on selecting the `UIElement` with the script so we can see where the problem is – Alex Cio Jul 23 '15 at 09:14
  • 1
    Oky I post the screenshots to my original question. – Hendrien Jul 23 '15 at 09:24
0

Change InputField(TMP) to InputField(Legacy)

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 30 '22 at 08:24