unity-game-enginetextmeshpro

Unity TextMeshPro input always has a length of 1 even after doing Trim()


I have a text input from TextMeshPro that's empty and when I try to grab the text, even after using trim, the length is always 1. There's an invisible character that I can't get rid of.

public class MainMenuController : MonoBehaviour
{
    public TextMeshProUGUI codeInput;

    private void Start()
    {
        string trimmedText = codeInput.text.Trim();
        int textLength = trimmedText.Length;
        Debug.Log(textLength);
    }
}

I have done Trim() and tried assigning the text in the input with "" or string.Empty or with a random string then "". Still always a length of 1.

I've looked up similar problems but the answer is just use Trim() but it does not work for me. Would appreciate some insight on this


Solution

  • There is always an extra hidden white-space character in Textmeshpro display text. I don't know why but there's talk about it here: https://forum.unity.com/threads/textmesh-pro-ugui-hidden-characters.505493/

    Generally you should not read from display text. You should be able to access the correct string by referencing the Text Mesh Pro Input Field component, as opposed to the text component which is displaying the input.

      string text = tmp_inputField.text;
    

    This is standard practice because the the display text often alters the actual input in other ways such as masking a password.