Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

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

Discussion in 'UGUI & TextMesh Pro' started by tormodvh, Sep 14, 2022.

  1. tormodvh

    tormodvh

    Joined:
    Jun 10, 2022
    Posts:
    3
    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<InputField>(); 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
     
  2. christianstrang

    christianstrang

    Joined:
    Aug 6, 2013
    Posts:
    29
    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?
     
  3. tormodvh

    tormodvh

    Joined:
    Jun 10, 2022
    Posts:
    3
    Thanks for you effort! :)
    The text field is visible and works as input. GetComponent<InputField>() returns Null so I get an error on Null reference.

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



    upload_2022-9-14_10-38-23.png upload_2022-9-14_10-38-58.png upload_2022-9-14_10-48-11.png
     

    Attached Files:

  4. christianstrang

    christianstrang

    Joined:
    Aug 6, 2013
    Posts:
    29
    What if you changed line 3 with that?
    TMP_InputField ifif = ifgo.GetComponent<TMP_InputField>();


    Might have to include
    using TMPro;
    first
     
    tormodvh likes this.
  5. DCMonkey

    DCMonkey

    Joined:
    Jun 21, 2015
    Posts:
    11
    That hierarchy under NameField looks like a TextMeshPro input field. Try getting a TMPro.TMP_InputField component instead.
     
    tormodvh likes this.
  6. tormodvh

    tormodvh

    Joined:
    Jun 10, 2022
    Posts:
    3
    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 :)


    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;
     
  7. christianstrang

    christianstrang

    Joined:
    Aug 6, 2013
    Posts:
    29
    tormodvh likes this.