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");
           }
    }
}

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;

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.

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;

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