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

I am getting this error when i try to parse a string to get float value.

Discussion in 'Editor & General Support' started by Shabih554, Aug 2, 2019.

  1. Shabih554

    Shabih554

    Joined:
    May 15, 2017
    Posts:
    11
    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, System.IFormatProvider provider)

    The Simple Script:

    Code (CSharp):
    1. public Text ResultsBaseRes;
    2. float res=0;
    3. public void Create()
    4. {
    5. res = float. Parse(ResultsBaseRes.text);
    6. }

    What I have already Tried?

    changing the line to

    Code (CSharp):
    1. res = float.Parse(ResultsBaseRes.text, CultureInfo.CreateSpecificCulture("en-US"));
    2. res = float.Parse(ResultsBaseRes.text, CultureInfo.InvariantCulture.NumberFormat);
    3. res = float.Parse(ResultsBaseRes.text, CultureInfo.InvariantCulture);
    Unity Version: 2019.1.08f

    Please help!!!
     
  2. Shabih554

    Shabih554

    Joined:
    May 15, 2017
    Posts:
    11
    Edit:

    I have also tried


    Code (CSharp):
    1. res = Convert.ToSingle(ResultsBaseRes.Text);
    Also the Input is in the correct format as the Input Feild has been set to take decimal numbers only.
     
    Last edited: Aug 2, 2019
  3. Shabih554

    Shabih554

    Joined:
    May 15, 2017
    Posts:
    11
    Well mine is simple i just want to parse a decimal value that is going to be between 0 and 50
    but i get this exception every time i try to parse.
     
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,350
    what is ResultsBaseRes?

    and can you show the string, maybe it has spaces or other extra characters somewhere?
     
  5. Shabih554

    Shabih554

    Joined:
    May 15, 2017
    Posts:
    11
    Its Just an Input Field. Its pre-configured to only take in decimal numbers
     
  6. Shabih554

    Shabih554

    Joined:
    May 15, 2017
    Posts:
    11
    God Dammit !! XD i was taking in the wrong field
     
  7. Abba13

    Abba13

    Joined:
    Sep 30, 2020
    Posts:
    5
    what do you mean?
     
    Vi1ton likes this.