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

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

Discussion in 'UGUI & TextMesh Pro' started by markashburner, Jul 29, 2019.

  1. markashburner

    markashburner

    Joined:
    Aug 14, 2015
    Posts:
    212
    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 <df7127ba07dc446d9f5831a0ec7b1d63>:0)
    System.Single.Parse (System.String s, System.Globalization.NumberStyles style, System.Globalization.NumberFormatInfo info) (at <df7127ba07dc446d9f5831a0ec7b1d63>:0)
    System.Single.Parse (System.String s) (at <df7127ba07dc446d9f5831a0ec7b1d63>:0)
    UniverseGenerator.GalaxyPlayerCamera.FixedUpdate () (at Assets/_Scripts/Galaxy/GalaxyPlayerCamera.cs:399)


    The code I am using is this:

    Code (CSharp):
    1. speed = float.Parse(normalSpeedUI.text);              
    2. 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?
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    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.
     
    cmembrez, rugi81, StarArcher and 5 others like this.
  3. MrG

    MrG

    Joined:
    Oct 6, 2012
    Posts:
    363
    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.
     
    Aber14 likes this.
  4. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    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 "*****".
     
    StarArcher likes this.
  5. naviln

    naviln

    Joined:
    Oct 18, 2016
    Posts:
    32
    Very handy and useful to know, thank you!
     
  6. efthymiougiorgos

    efthymiougiorgos

    Joined:
    Apr 7, 2020
    Posts:
    2
    Useful info! Thanks!
     
  7. METALNAVY

    METALNAVY

    Joined:
    May 8, 2022
    Posts:
    1
    THANK YOU SO MUCH !!!!
    (i literally spent 3 hours searching for this)
     
  8. Ottiotta88

    Ottiotta88

    Joined:
    Jul 7, 2022
    Posts:
    1
    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 use
    System.Convert.ToDouble
    and then convert to a float (I do not know how)
     
    Last edited: Dec 22, 2022
  9. Aber14

    Aber14

    Joined:
    Jul 22, 2022
    Posts:
    2
    Helpful! Thankyou So Much