Rob W
Rob W

Reputation: 611

Access the TM Pro InputField text in Unity

I've been working on this solidly for hours and can't seem to get anywhere, so your help will be much appreciated. I have created a few TextMeshPro UI InputFields in Unity and can't seem to access the text after a user has inputted something. I can do it when it's a normal InputField but not with the TM Pro version. I've created a Serializefield of type TMP_InputField which I have hooked up in the inspector (also including using TMPro namespace). Could somebody please outline the steps required to get the text into a variable because I am stumped! The script is currently sitting on a different GameObject. I have an array of type TMP_InputField called theFields and when I debug theFields[0].name that seems to work fine (using FindObjectsOfType<>) but when I try to just access a specificField.text it throws a null reference objection when trying to edit the field during the game. Any tutorials/support would be very welcome!

Upvotes: 2

Views: 10532

Answers (3)

MCroswell
MCroswell

Reputation: 11

I just code it up and then in the editor drag the TMP_InputField object into the corresponding slots in the script.

So, code-wise, I would have script like Milorad Zivanovic shows.

If you want multiple ones you might have: public TMP_InputField [] myInputs;

In the editor (as described above in my first sentence) you just hit the plus button and drag them in. Or just highlight them all and drag in all at once. If you lock your object in the editor to do this (little lock icon up top right) don't forget to unlock it.

To access in your code you either loop or use an index. Like to get the first one you would do this: {//... in some method

myInputs[0].text = "something"; }

PS: I did not have to explicitly set the interactable field.

PPS: The answer that HyoJin KIM gave is great and best if you like using Unity's Event. I just gave this answer so you'd have an additional way of doing it that might match your original attempt(s).

Be aware that if you do choose this latter approach it is very easy to make the mistake of choosing the static version of the same method (they are listed twice!) Many times I make this mistake. Choose the top one (non-static, aka Dynamic) for most cases! My scenario using events

Upvotes: 0

Milorad Zivanovic
Milorad Zivanovic

Reputation: 21

using TMPro;


public TMP_InputField TMP_IF;

TMP_IF.interactable = true;

TMP_IF.text = "whatever you want";

For those interested in year 2020 :) Seems that property interactable is instantly disabled, solution was to enable it so it text could be updated...

Upvotes: 2

HyoJin KIM
HyoJin KIM

Reputation: 402

Did you try with UnityEvents fields on the Inspector of TMP_InputField?

I make a simple script and it combines with TMP_InputField

using UnityEngine;

public class InputfieldListener : MonoBehaviour
{
    public void OnChangedInputField(string input)
    {
        Debug.Log("[OnChangedInputField] " + input);
    }

    public void OnEndedInputField(string input)
    {
        Debug.Log("[OnEndedInputField] " + input);
    }

    public void OnSelectedInputField(string input)
    {
        Debug.Log("[OnSelectedInputField] " + input);
    }

    public void OnDeslectedInputField(string input)
    {
        Debug.Log("[OnDeslectedInputField] " + input);
    }
}

Inspector of TMP_InputField

Then you can find the log of each event.

FYI, the creator has a youtube channel and there is a related video tutorial.

Upvotes: 1

Related Questions