Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    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. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    10,004
  4. 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.
     
  5. mgear

    mgear

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

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

    Shabih554

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

    Shabih554

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

    Abba13

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