How to get Text from TextMeshPro input field

I cannot understand why the string username is not equal with "a". Even if I enter the string "a" in the input field.

Can anyone help me?

using TMPro;

public class ButtonsScript : MonoBehaviour
{
    public TextMeshProUGUI register_username; //drag and drop element
    public void getValue ()
    {
           string username = register_username.text; //here the value is "a"
           if(username.Equals("a"){  //however here does not find it equal
                   Debug.Log("here");
           }
    }
}

7 Answers

7

Here are a few things to make this work in version 2019.4.0f1

  1. using TMPro;
  2. GameObject inputField; // This should be the input parent field and not the child text field. See this post for more details.
  3. string text = inputField.GetComponent<TMP_InputField>().text;

Unity Answers changed the URL. I can't control it.

Worked for me TMP_InputField was the key solution

A bit old, but still @bigbosss
Another forum answer was:

You need to access the text of the TextmeshPro input field from the TMP_InputField class as TMP_InputField.text

If you access the text directly from the text component it will not give the result you want.

The link you gave return to here

Your link redirects here......................

To help clarify: this.GetComponentInChildren<TMP_Text>().text accesses the Placeholder this.GetComponentInChildren<TMP_InputField>().text accesses the Text [185435-screen-shot-2021-08-28-at-53457-am.png|185435]

Instead of declaring it with

TextMeshProUGUI register_username

use

TMP_Text register_username;

if you have a Text - Text Mesh Pro in your scene

using TMPro;

public TMP_InputField register_username;

string username = register_username.text;

Excellent, thanks. This worked for me after spending hours trying to figure out why I had trailing zero width spaces! > %E2%80%8B

Excellent, thanks. This worked for me after spending hours trying to figure out why I had trailing zero width spaces! > %E2%80%8B

Thanks very much!

So this doesn’t exactly work. What you need to do is grab the TextMeshProUGUI, which I would demonstrate but this apparently hates angle brackets.

There’s a much more in depth explanation Here

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
  
public class TMPInputFieldDemo : MonoBehaviour
{
    public GameObject TMP_InputField_Username;

    void Update()
    {
        GetUsername();
    }
  
    public void GetUsername() {
        string name = TMP_InputField_Username.GetComponent<TMP_InputField>().text;
        Debug.Log("Username: " + name);
    }
}

There might be some trailing no-break-spaces. You need to trim before parsing.
And use TMP_InputField

Example:

public class YourClass : MonoBehaviour
{
    TMP_InputField yourInputField;
    void Start()
    {
        float yourNumber = float.Parse(yourInputField.text.Trim());
    }
}

there is a wierd no breaking space at the end :confused:
you probably need to trim before confronting variables