Search Unity

How to get input text field text?

Discussion in 'UGUI & TextMesh Pro' started by LegendWill, Oct 4, 2014.

  1. LegendWill

    LegendWill

    Joined:
    Jun 20, 2014
    Posts:
    9
    So, how do you get the text that the user puts into the input text field and then, I don't know, log it?
    Like a test debug.log(usernameText)
    Or something like that
     
  2. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    By inputfield.value

    For example, make a variable at the top where you reference your input field. Then you can access the text of it by this:
    Code (csharp):
    1.  
    2. var myinputfield:UI.InputField;
    3.  
    4. ... do something
    5.  
    6. Debug.Log (myinputfield.value);
    7.  
    Interestingly this doesn't work with the initial value at start of frame. Smelling a bug here ...
     
  3. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    I dont think its a but, if nothing have been typed in the inputfield its value is "" and only showing some default text from the text component.
     
  4. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    Hmm. Dunno if it is that easy. There is also a starting value. Means there is already content in the Input field when this is filled, not just text in the text component.
     

    Attached Files:

  5. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    Setting the starting value should set the text value as well, but if left blank then the Text.text will be what is defaulted to. But now that i think of it that seems a little weird so i'll likely fix it so its only the starting value that is used.
     
  6. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    Sounds like a good idea. I already wondered about that :)
     
  7. ChoMPi

    ChoMPi

    Joined:
    Jul 11, 2013
    Posts:
    112
    Maybe you should create a placeholder feature, when the values is "" display the place holder...
     
  8. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    yea the starting value is what is considered a place holder for a "" input string. So your starting value could be "User Name" and when the user types it then becomes their user name if at any point its empty again upon deselection it becomes "User Name" again.
     
  9. ChoMPi

    ChoMPi

    Joined:
    Jul 11, 2013
    Posts:
    112
    I am not sure why but in b20 it does not behave anything like that... It behaves just like the value that is set at start, was that changed ?

    Edit: Ops i found out what's happening, if you deselect the input field with the mouse and not with submitting it the Starting value is not applied but instead it's left empty...