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.
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.
@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?
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.
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…
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
Solution: get the string not from the “TextMeshProUGUI” component. u have to take it from “TMP_InputField” component
example:
public void InputField_on_value_changed()
{
string txt = gameObject.GetComponent<TMP_InputField>().text;
}
1 Like