.GetComponent<InputField>() returns NULL on Gameobject(InputField)

I’m not able to get the InputField object in any way.

My Canvas has an input field named “NameField” and I get it by calling GameObject.Find(“NameField”);

Then I try to get the input field by calling GetComponent(); on the gameobject with no luck.

I have looked up about 50 different examples; all refers to InputField, witch is the object I can’t find and find no examples to get.

Thanks for helping me understand…

Tormod

What happens when you, for example, call GetComponent<InputField>().text = "test"?
Do you get an error? Is your InputField visible and can you place text via the unity editor in the “Text” Gameobject that is a child of the InputField?

Thanks for you effort! :slight_smile:
The text field is visible and works as input. GetComponent() returns Null so I get an error on Null reference.

Code:
GameObject ifgo = GameObject.Find(“NameField”); // OK
Debug.Log(ifgo);
InputField ifif = ifgo.GetComponent(); //returns NULL
Debug.Log(ifif);
ifif.text = this.playerName;

8438621--1118141--upload_2022-9-14_10-38-23.png8438621--1118144--upload_2022-9-14_10-38-58.png8438621--1118165--upload_2022-9-14_10-48-11.png

8438621--1118144--upload_2022-9-14_10-38-58.png

What if you changed line 3 with that?
TMP_InputField ifif = ifgo.GetComponent<TMP_InputField>();

Might have to include using TMPro; first

1 Like

That hierarchy under NameField looks like a TextMeshPro input field. Try getting a TMPro.TMP_InputField component instead.

1 Like

Thank you very much both of you. Now it works as a dream.

Yes it is an IntputField (TextMeshPro) from Unity 2022.(something)…
My only worry is how important this feature is, and still so hard to find an explanation :slight_smile:

Working code for writing to input field:
using TMPro;


GameObject ifgo = GameObject.Find(“NameField”);
Debug.Log(ifgo);
TMP_InputField ifif = ifgo.GetComponent<TMP_InputField>();
Debug.Log(ifif);
ifif.text = this.playerName;

Just make sure to search for “textmeshpro inputfield” etc to get good results via google, also:
https://docs.unity3d.com/Packages/com.unity.textmeshpro@1.3/api/TMPro.TMP_InputField.html

1 Like

Thank you for this information I am using Unity in 2024 and could not get Text or TextFields to change text until I found this post, so thanks.

So now I can get a child of a canvas in unity and set its properties such as .text

For any interested or having troubles this might help you when it comes to updating a textfield in unity or changing text in unity ( assuming you added the TextField in the Unity UI ) or ( assuming you added the Text in the Unity UI ).

First, a C# utility function to get any child of a Canvas:

using UnityEngine;
 
public static Transform getCanvasChild(string canvas, string childName)
    {
        var rootCanvas = GameObject.Find(canvas);
        var currentCanvas = rootCanvas.GetComponent<Canvas>();
        if(currentCanvas is not null)
        {
            var myTransform = currentCanvas.GetComponentsInChildren<Transform>();
            foreach (var target in myTransform)
            {
                if(target.name.Equals(childName))
                {
                    return target;
                }
            }          
        }
        return null;
    }

C# Code using the above function:

using TMPro;

private void clearText(string canvas)
    {
        var inputTransform = Game.getCanvasChild(canvas, "InputField");
        TMP_InputField currentInput = inputTransform.GetComponent<TMP_InputField>();
        currentInput.text = "put text here";
    }