Search Unity

InputField clear text

Discussion in 'UGUI & TextMesh Pro' started by Edgaras-Randis, Sep 8, 2014.

  1. Edgaras-Randis

    Edgaras-Randis

    Joined:
    Jul 8, 2014
    Posts:
    20
    What's the best way to clear input field text after onSubmit event was fired?

    I've tried:

    Code (CSharp):
    1.  
    2. void OnSubmitEvent(string Message)
    3. {
    4.      MyInputField.text.text = "";
    5. }
    6.  
    which gives no result
     
  2. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,037
    Not having much success with this either. I tried changing value instead of text.text, and that had some effect. When not selected the text now says "Input", but when you click in it, it appears blank. The OnSubmit handler doesn't seem to work at all either, so there's a lot of wonky stuff with this widget.
     
  3. Edgaras-Randis

    Edgaras-Randis

    Joined:
    Jul 8, 2014
    Posts:
    20
    OnSubmit handler works for me, but I have no success of clearing the text input.
     
  4. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    You need to set the InputFields value not the text.text value. this is because the Inputfield uses the text to display some portion of its value (depending on what is visible).

    @orb once you clear the input field it defaults back to its "empty" value. which is the value when the Input field inits. If you want it to be blank blank I might need to expose the empty value (or just have it start blank). OnSubmit should work. what platform are you using?
     
  5. Edgaras-Randis

    Edgaras-Randis

    Joined:
    Jul 8, 2014
    Posts:
    20
    Thanks, your suggestion was correct.
     
  6. ortin

    ortin

    Joined:
    Jan 13, 2013
    Posts:
    221
    I would say just expose it, as it's useful feature to have input field "saying" something when it's empty as opposed to starting value which should disappear after user first time clicked. Also both should have color property exposed.

    Though may be it's better idea to add "persistent" check box to it and have only one "Empty value" and it will either disappear after input field became active first time or will return each time when it's empty.
     
    orb likes this.
  7. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,037
    Cheers, I'll have a poke at it again later. This is on OS X 10.9.4, only tested in the editor. I should probably check standalone, because different behaviour isn't that rare when I try that ;)
     
  8. fatheadx

    fatheadx

    Joined:
    Mar 2, 2016
    Posts:
    10
    Hi Guys,

    I am trying to clear the InputField. I call this ClearImageText() through a button which should clear the input field. But it is not working. Any Ideas why?

    I am using 2019.1.13f1

    public TextMeshProUGUI imageCaptionInputField;
    public TextMeshProUGUI imageCaptionText;

    public void ClearImageText()
    {

    imageCaptionInputField.text = string.Empty;
    imageCaptionText.SetText("");
    imageTextPanel.SetActive(false);
    Debug.Log("Cleared Image Caption" + imageCaptionInputField.text.Length);

    }
     
    samra2494 and masterkonnen_unity like this.
  9. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Clear the text of the TMP_InputField itself not the text of the child text object.
     
  10. fatheadx

    fatheadx

    Joined:
    Mar 2, 2016
    Posts:
    10
    Thank you Stephan_B. It worked. Here is what I did:

    imageCaptionInputFieldParent.text = "";