Search Unity

Question Remove blank-space from InputField.TMP

Discussion in 'UGUI & TextMesh Pro' started by jeffreyshoe1, Dec 15, 2022.

  1. jeffreyshoe1

    jeffreyshoe1

    Joined:
    Oct 31, 2022
    Posts:
    7
    I am trying to get the text from the InputField as a integer, but for some reason the text always includes a blank-space at the end.

    Things I've tried:
    replace - didn't work as it doesn't recognize it as blank space
    int.TryParse - returns false because of the blank space

    Code (CSharp):
    1. public TMP_Text livesSet; //is a child of InputField
    2. foreach(var i in livesSet.text)
    3.         {
    4.             Debug.Log(i);
    5.         }
    InputField text is set to "3", however it is being read as "3 "


    Output:

    3
    UnityEngine.Debug:Log (object)
    MainMenu:Start () (at Assets/Scripts/MainMenu.cs:25)

    UnityEngine.Debug:Log (object)
    MainMenu:Start () (at Assets/Scripts/MainMenu.cs:25)

    How would I get rid of it?
     
    Last edited: Dec 15, 2022
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    The child component of the text input field should not be accessed directly. This child component is used for display only and contains formatting characters. For instance, when the input field is set to password, the child component will contains a bunch of "*****" whereas the TMP_InputField.text property will contains the relevant text.

    In short you should be accessing the parent input field text property to check its content.

    There are several posts on the forum which may contain more information about this topic.
     
    jeffreyshoe1 likes this.