How to get Text from TextMeshPro input field
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");
}
}
}
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?
Answer by nmill99 · Jul 03, 2020 at 11:33 PM
Here are a few things to make this work in version 2019.4.0f1
using TMPro;
GameObject inputField; // This should be the input parent field and not the child text field.
See this post for more details.string text = inputField.GetComponent<TMP_InputField>().text;
Answer by OleIvanoff · Mar 10, 2019 at 11:08 AM
A bit old, but still @bigbosss Another forum answer was: https://answers.unity.com/questions/1562568/how-to-get-text-from-textmeshpro-input-field.html
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.
To help clarify:
this.GetComponentInChildren<TMP_Text>().text accesses the Placeholder
this.GetComponentInChildren<TMP_InputField>().text accesses the Text
Answer by Nick_Laza · Feb 05, 2020 at 04:18 PM
Instead of declaring it with TextMeshProUGUI register_username, use TMP_Text register_username; if you have a Text - Text Mesh Pro in your scene
Answer by SmulDev · May 03 at 01:24 PM
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
Answer by OrbitingOrange · Aug 02, 2019 at 12:04 AM
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