Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

Clearing Text Value

Discussion in 'UGUI & TextMesh Pro' started by Lypheus, Aug 22, 2014.

  1. Lypheus


    Apr 16, 2010
    I have an Input Field on my scene (in this case a login screen) and would like to ensure that if the user selects a field, it removes the existing text by clearing the fields current value. The following code works, however it exhibits the following behaviour:

    Stage this by having no starting value in InputField, and setting text property of Text child object to "Username".

    1. Clear the text from the field.
    2. Since it is empty, show the value of text (i.e. "Username").
    3. Focus gets set to the field.
    3. Clear the text from the field now that value is seen as "" and focus is in the field.

    Code (csharp):
    2. using UnityEngine;
    3. using UnityEngine.UI;
    5. public class EventHandler : MonoBehaviour
    6. {
    7.     public void OnFocusUserName( GameObject userNameField )
    8.     {
    9.         InputField inputField = userNameField.GetComponentInChildren<InputField>( );
    10.         inputField.value = "";
    11.     }
    12. }
    My question is: How do I clear the text from a Input Field *without* having the default Text value flash by?

    To save time - a workaround using this code is to set "Starting Value" and leave the text property of the child Text object empty. That means I'm actually setting a "value" however and is *not* the same scenario being described.

    Basically I'd like to be able to clear the value from Input Field and not have any visual side effects from the "mask text".
    rakkarage likes this.
  2. phil-Unity


    Unity UI Lead Developer

    Nov 23, 2012
    Hm I understand your question but don't have a good solution right now. Let me look into this Monday once I'm home from unite and I'll get back to you
  3. Lypheus


    Apr 16, 2010
    Sounds good, have fun!