[solved] float.Parse(string) not working with text from a unity input field

Here’s my code:

When I call the Move() function, I get this error:
System.FormatException: Input string was not in a correct format.
at System.Number.ParseSingle (System.String value, System.Globalization.NumberStyles options, System.Globalization.NumberFormatInfo numfmt) [0x00083] in <695d1cc93cca45069c528c15c9fdd749>:0
at System.Single.Parse (System.String s, System.Globalization.NumberStyles style, System.Globalization.NumberFormatInfo info) [0x00000] in <695d1cc93cca45069c528c15c9fdd749>:0
at System.Single.Parse (System.String s) [0x0000b] in <695d1cc93cca45069c528c15c9fdd749>:0
at OnClickRobot.Move () [0x00020] in D:\Educational Game\Educational GAme\Assets\Scripts\OnClickRobot.cs:75
UnityEngine.Debug:LogError (object)
OnClickRobot:Move () (at Assets/Scripts/OnClickRobot.cs:83)
UnityEngine.EventSystems.EventSystem:Update () (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:385)

But as you can see from when I log the texts, they should be valid, parsable strings…?

What is going on here?

Are you accessing the child Text object? That’s a no-no. It is for presentation to the user.

Access the field of the InputField object itself.

When in doubt, assign it to a string and print its length.

If the length of “123213” is anything except 6, you know there are hidden characters.

Oh, that fixed it! I didn’t realize that I needed to grab the InputField instead of the text. Thank you so much!

1 Like