float.Parse does not work in TMPro Input field which basically means TMPRo is useless!

Hi

I am trying to achieve an extremely simple thing…all I want to do is basically set the speed of my camera in an input field. So I have setup an input field using TMPPro and guess what?

Just errors…

FormatException: Input string was not in a correct format.
System.Number.ParseSingle (System.String value, System.Globalization.NumberStyles options, System.Globalization.NumberFormatInfo numfmt) (at :0)
System.Single.Parse (System.String s, System.Globalization.NumberStyles style, System.Globalization.NumberFormatInfo info) (at :0)
System.Single.Parse (System.String s) (at :0)
UniverseGenerator.GalaxyPlayerCamera.FixedUpdate () (at Assets/_Scripts/Galaxy/GalaxyPlayerCamera.cs:399)

The code I am using is this:

speed = float.Parse(normalSpeedUI.text);               
fastSpeed = float.Parse(fastSpeedUI.text);

I have also set the content type to Decimal Number.
that’s it…that’s all I am trying to achieve and for the last 2 hours…can not work it out…so basically TMPro is useless…I mean if it is this difficult to parse a float value from string using TMPro then what’s point in this asset?

Make sure you are grabbing the text from the Input Field Component and not from the child Text Component.

The child text component contains text for visualization purposes with extra formatting characters. For example when using password mode, the child contains “******” whereas the TMP_InputField.text property contains the actual password.

10 Likes

Perhaps related…

manager.networkAddress = IPField.GetComponent<TMPro.TextMeshProUGUI>().text.Trim();

…includes a Unicode Zero Width Space (U+200B) on the end that Trim() doesn’t remove.

1 Like

That is correct in terms of why the float.Parse would fail. However, and more importantly, this is due to referencing the child text component instead of the parent TMP_InputField and its .text property.

As I stated in my previous post, the child text component contains formatting characters and in many cases not the actual text like in the case of a password or pin where the child component .text property would contain a bunch of “*****”.

1 Like

Very handy and useful to know, thank you!

Useful info! Thanks!

THANK YOU SO MUCH !!!
(i literally spent 3 hours searching for this)

Yes, maybe parse does not work but System.Convert.ToInt32 does it
Example:System.Convert.ToInt32(LevelBalls.GetComponent<TMP_InputField>().text); and it works!

Floats maybe is not working this way but doubles are so you can useSystem.Convert.ToDouble and then convert to a float (I do not know how)

Helpful! Thankyou So Much