Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Set Text of Input Field?

Discussion in 'UGUI & TextMesh Pro' started by Tiles, Sep 23, 2014.

  1. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    I can read the content of an Input Field.

    mystring.text=inputfield.text.text;

    But i need to change the content of the input field. And this one does not work. It doesn't give a warning, but simply refuses to set the text:

    inputfield.text.text=mystring.text;

    Is it even possible to change the text of the input field?
     
  2. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    inputfield.value is what you want to look at. The text.text will not be the full value of the input field but only the visible portion
     
  3. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    Many thanks, works like charm :)
     
  4. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    Hm, nope, I get an error now from time to time:
    Code (CSharp):
    1. System.ArgumentOutOfRangeException: Argument is out of range.
    2. Parameter name: index
    3.   at System.Collections.Generic.List`1[UnityEngine.UICharInfo].get_Item (Int32 index) [0x0000c] in /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/List.cs:633
    4.   at UnityEngine.UI.InputField.CalculateTextVisibility (UnityEngine.TextGenerator result, System.Int32& newDrawStart, System.Int32& newDrawEnd) [0x001f5] in C:\BuildAgent\work\d63dfc6385190b60\Extensions\guisystem\guisystem\UI\Core\InputField.cs:1261
    5.   at UnityEngine.UI.InputField.UpdateLabel () [0x00121] in C:\BuildAgent\work\d63dfc6385190b60\Extensions\guisystem\guisystem\UI\Core\InputField.cs:1074
    6.   at UnityEngine.UI.InputField.DeactivateInputField () [0x0005f] in C:\BuildAgent\work\d63dfc6385190b60\Extensions\guisystem\guisystem\UI\Core\InputField.cs:1638
    7.   at UnityEngine.UI.InputField.OnDeselect (UnityEngine.EventSystems.BaseEventData eventData) [0x00007] in C:\BuildAgent\work\d63dfc6385190b60\Extensions\guisystem\guisystem\UI\Core\InputField.cs:1650
    8.   at UnityEngine.EventSystems.ExecuteEvents.Execute (IDeselectHandler handler, UnityEngine.EventSystems.BaseEventData eventData) [0x00000] in C:\BuildAgent\work\d63dfc6385190b60\Extensions\guisystem\guisystem\EventSystem\ExecuteEvents.cs:108
    9.   at UnityEngine.EventSystems.ExecuteEvents.Execute[IDeselectHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) [0x00087] in C:\BuildAgent\work\d63dfc6385190b60\Extensions\guisystem\guisystem\EventSystem\ExecuteEvents.cs:257
    10. UnityEngine.EventSystems.EventSystem:Update()
    11.  
     
  5. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    Then thats a bug, could you submit your project that causes this.
     
  6. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    Yes, looks like a bug. Will have a look to reproduce it in an example file. The current project is already a bit too big to be useful for a bug case.

    Please give me a few hours to sort things. I'll submit it then, and will place the case number in this thread.
     
  7. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    I have reduced the problem to a small example file. The cause seems to be the character limit. I have submitted the problem as a bug report yesterday. But didn't receive a message from QA yet what bug number it is. So just in case, here comes the dataset as an attachment too. The example project and a screenshot.

    Load the project, run the app, in the input field type in a value that is above the character limit of the input field, and you will get a out of range exception when you hit enter.
     

    Attached Files:

  8. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    perfect that helps alot :)
     
  9. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    Hm, seems that the bug didn't made it into the tracker. Two days gone, no confirmation mail. Should i resubmit the bug report? Or is this thread here enough?
     
  10. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    id like to think this thread is enough, but i have a bad feeling i'll forget about it in amongst the rest of the work being done.
     
  11. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    Last edited: Sep 26, 2014
    AlanMattano likes this.
  12. Qwolf

    Qwolf

    Joined:
    Feb 1, 2014
    Posts:
    28
    I can't seem to get GetComponent<InputField>().value to work? Any Ideas?
     
  13. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,647
    Send me a message if you ever figure this out. Been trying to figure this out for days now
     
  14. LunaticEdit

    LunaticEdit

    Joined:
    May 1, 2014
    Posts:
    60
    Wouldn't it make more sense to hook into the OnValueChange event?
     
  15. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,647
    isn't onvaluechange event only when the value is being changed? The problem im having is I can't even select it to change it
     
  16. LunaticEdit

    LunaticEdit

    Joined:
    May 1, 2014
    Posts:
    60
    You mean you can't even see text? Make sure you add a Layout Element and set a MinHeight to something high enough to prevent the text object from clipping.
     
  17. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,647
    When I click on user name there is no carrot blinking can't type in it.
    Just keeps the placeholder


     
  18. LunaticEdit

    LunaticEdit

    Joined:
    May 1, 2014
    Posts:
    60
    What is that "input manager" component?
     
  19. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,647
    empty script. I added it cause was trying to activate the text field to make it selectable lol
     
  20. LunaticEdit

    LunaticEdit

    Joined:
    May 1, 2014
    Posts:
    60
    I hate to cross-promote, but maybe use some UI wrapper library that handles the crazy for you? ;)

    I noticed your Pos Y is -300, is this expected? Also check the design tab and make sure the text field and its children are where you think they are. Sometimes you'll see they have a height of 0, or are way out in lala land outside the camera.
     
  21. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,647
    Can you give me some example UI wrapper libraries that I could use.
    Ya I was wondering about the -300 too. for some reason its negative to move something down.
    Like on CSS to move down its positive. I'll try to locate the design tab
     
  22. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,647
    For some reason when I click on user name input field right now I get
    Object reference not set to an instance of an object
    This just started happening
     
  23. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,647
    This is what it looks like selected. Also disabled the button and other input field too just incase they were some how over lapping stuff idk lol.


     
  24. LunaticEdit

    LunaticEdit

    Joined:
    May 1, 2014
    Posts:
    60
    Look at the top of this forum about the Xml framework I'm working on :p

    If you double-click that error, it should open the file and line it is breaking on. Something tells me there's more going on than first meets the eye.
     
  25. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,647
    Its not a error from one of my scripts though. Double clicking doesn't do anything.
    Maybe something is just wrong with the trial pro version of the engine lol.




    NullReferenceException: Object reference not set to an instance of an object
    UnityEngine.TextGenerator.get_characterCountVisible () (at C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/GUIElementBindings.cs:552)
    UnityEngine.UI.InputField.GenerateCursor (System.Collections.Generic.List`1 vbo, Vector2 roundingOffset) (at C:/BuildAgent/work/d63dfc6385190b60/Extensions/guisystem/guisystem/UI/Core/InputField.cs:1620)
    UnityEngine.UI.InputField.OnFillVBO (System.Collections.Generic.List`1 vbo) (at C:/BuildAgent/work/d63dfc6385190b60/Extensions/guisystem/guisystem/UI/Core/InputField.cs:1591)
    UnityEngine.UI.InputField.UpdateGeometry () (at C:/BuildAgent/work/d63dfc6385190b60/Extensions/guisystem/guisystem/UI/Core/InputField.cs:1533)
    UnityEngine.UI.InputField.Rebuild (CanvasUpdate update) (at C:/BuildAgent/work/d63dfc6385190b60/Extensions/guisystem/guisystem/UI/Core/InputField.cs:1501)
    UnityEngine.UI.CanvasUpdateRegistry.PerformUpdate () (at C:/BuildAgent/work/d63dfc6385190b60/Extensions/guisystem/guisystem/UI/Core/CanvasUpdateRegistry.cs:103)
    UnityEngine.Canvas:SendWillRenderCanvases()
     
  26. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,647
    I downloaded that plug in and the input field works on that lol. Not sure what im doing different. all my components are the same.
     
  27. LunaticEdit

    LunaticEdit

    Joined:
    May 1, 2014
    Posts:
    60
    You could also make sure it isn't something odd/dumb like not being place on a gameobject with an image attached or something. I know, for example, the mask component only works if the game object also has an image component, and only if that image component has an alpha of at least 0.03-0.05f...
     
  28. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,647
    How are you able to get text from a inputfield though
    sitting here googling this for hours.

    assuming that I'm even grabing the game object. This is one thing that bothers me the most with learning Unity.
    Why can't I do Debug.Log(username_object);
    It seems I can never debug objects? How do you print out what is inside a object? If I could just do that then I could finally see what is going on? Right now its like Im running around in the dark.

    Coming from a JavaScript/php back ground I can do Console.Log or in php Var_dump and see everything a variable holds. This is really the largest problem I have.

    Code (csharp):
    1.  
    2. #pragma strict
    3. import UnityEngine.UI;
    4.  
    5. var username_object : GameObject;
    6. var username;
    7.  
    8. function Login(){
    9.          username_object = GameObject.Find("username");
    10.          username = username_object.GetComponent(Text);
    11.  
    12.          Debug.Log("user name is " + username);
    13. }
    14.  
    15.  
     
    Last edited: Feb 2, 2015
  29. giano574

    giano574

    Joined:
    Nov 28, 2013
    Posts:
    76
    You have to get the InputField, not the Text component.
    Code (JavaScript):
    1. username = username_object.GetComponent(InputField);
    2.  
    3. Debug.Log("user name is "+ username.text);
     
    teamstepson likes this.
  30. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,647
    Thanks for the reply Ginao
    I get hit with error 'text' is not a member of Object
    I also tried
    Debug.Log("user name is " + username.textComponent);

    Code (csharp):
    1.  
    2.  
    3. #pragma strict
    4. import UnityEngine.UI;
    5.  
    6. var username_object : GameObject;
    7. var username;
    8.  
    9. function Login(){
    10.          username_object = GameObject.Find("username");
    11.           username = username_object.GetComponent(InputField);
    12.          Debug.Log("user name is " + username.text);
    13. }
    14.  
    15.  



     
  31. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,647
    Ahh I figured it out.
    I had to change
    var username; to var username : InputField;

    Since username turns into a InputField first

    But now I also changed it to
    username = username_object.GetComponent(InputField).text;
    with out giving username a type of InputField.

    So all good. Thanks a lot for the help you two. Think im starting to understand this lol
     
  32. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,647
    I'd like to know how to see what is inside an object though? Any ideas by chance
     
    Last edited: Feb 2, 2015
  33. LunaticEdit

    LunaticEdit

    Joined:
    May 1, 2014
    Posts:
    60
    Remember objects are simply containers for Components. When you say object.text, it's a mnumonic for object.GetComponent<Text>(). Objects themselves are nothing more than named containers, there's literally nothing to see.
     
  34. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,647
    Ahh that explains a lot. Thanks a lot LunaticEdit
     
  35. WonderWorkshop

    WonderWorkshop

    Joined:
    Aug 28, 2014
    Posts:
    7
    I still saw the Augment out of range error on Unity 5.0.1f1. It appears sometimes when going in and out of Unity.
     
  36. masteryder

    masteryder

    Joined:
    Aug 15, 2016
    Posts:
    5
    For Unity 5.6.1f1 the way I do this is :
    gameObject.GetComponent<InputField>().text = "text to affect";
     
    EricTViking and efge like this.
  37. UnderShad

    UnderShad

    Joined:
    Apr 11, 2019
    Posts:
    2
    Thank you do worked smooth :)