Search Unity

Unity UI InputField OnValueChanged problem.

Discussion in 'UGUI & TextMesh Pro' started by Cursedth, Oct 31, 2017.

  1. Cursedth

    Cursedth

    Joined:
    Oct 4, 2016
    Posts:
    27
    Hey all.

    I have an InputField where players can enter their name and want to check the input for illegal characters as a Folder and File is going to be created from this input.

    Thing is..
    When I enter the first character, let say "a" and OnValueChanged is called it returns a string of length 0.
    When I then enter "b", string length becomes 1 and only "a" is returned to console output.
    When I enter a third letter "c", length becomes 2 and only "ab" is returned to console.

    Shouldn't it be like when I enter "a", string length should be 1?

    Is this a bug or am I missing something?

    Thanks in advance.
     
  2. Johannski

    Johannski

    Joined:
    Jan 25, 2014
    Posts:
    826
    Are you using unitys intern solution or textmeshpro?
    I just took a look at unitys input field's callbacks, and I think the one you want to use is InputField.OnValidateInput.
     
  3. Cursedth

    Cursedth

    Joined:
    Oct 4, 2016
    Posts:
    27
    @Johannski
    Sorry I don't know what you mean with intern solution or textmeshpro (wasn't that an asset?)

    But I am using the EventTrigger of the InputField in the inspector (intern?) and have set OnValueChange pointing to a Method in a script attached to the parent canvas. (this part works)

    I have made a workaround and check for illegal characters after Submitting and use that Method now to clear the textfield used to output the message if illegal characters were used, when the players starts typing again . (then OnValueChange works as I don't check for characters anymore and just clear the message)

    But the strange thing was it would miss the first character entered?
     
  4. Johannski

    Johannski

    Joined:
    Jan 25, 2014
    Posts:
    826
    Heyhey,

    Oh sorry, I guess I wasn't that clear. The subforum is Unity UI & Textmesh Pro (which was bought by Unity and has its own implementation of an Input Field). I just wanted to be sure to know what you use in order to help you.

    As I pointed out in the last post, take a look at ValidateInput instead of OnValueChanged.
     
  5. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    onEndEdit and onValueChanged show up in the inspector. onValidateInput doesn't.

    So you'll need to hook up onValidateInput in script.

    Another option is you could download the InputField source code and modify it to expose that (plus add an onBeginEdit event!) to the inspector.

    Of course, if you're going down that route, you might as well fix onValueChanged to include the latest character you just typed...
     
  6. GrayMonkey

    GrayMonkey

    Joined:
    Aug 24, 2013
    Posts:
    3
    Noob at all of this stuff so please forgive any errors or misunderstandings!

    I've just been searching for the exact same issue. Having searched around a little and messed around with the code it appears that my mistake was that I was passing the Text.text value rather than the InputField.text value. Once I changed this it all appears to be working correctly.

    HTH
     
  7. flynn200

    flynn200

    Joined:
    Sep 22, 2019
    Posts:
    1
    Solution: get the string not from the "TextMeshProUGUI" component. u have to take it from "TMP_InputField" component
    example:
    Code (CSharp):
    1.  public void InputField_on_value_changed()
    2.     {
    3.         string txt = gameObject.GetComponent<TMP_InputField>().text;
    4.     }
     
    ludocube likes this.