float.Parse no longer working

I had a working project with the following lines of code

public InputField mass;

float val = float.Parse(mass.text);

Pretty straighforward, an user puts an ammount of mass and it gets parsed from text to float, this was working perfecitly fine days ago, I was even able to export the project several times, no issues whatsover.

Today I returned to make some changes, before doing so I test it out and get this error.

FormatException: Invalid format.
System.Double.Parse (System.String s,
NumberStyles style, IFormatProvider
provider) (at
/Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Double.cs:209)
System.Single.Parse (System.String s)
(at
/Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Single.cs:183)
ControlMasa.Update () (at
Assets/Scripts/ControlMasa.cs:47)

I have no idea why it suddendly stopped working, not as if I had updated the version of Unity or anything, one day it was working and the next it wasn’t.

What’s the problem, what can I do?

What exactly have you typed in? Note that the float format the way you parse it depends on the local culture settings of your OS. So for example if you are in germany or france the decimal point is a comma, not a dot. To parse (and also use ToString) with an invariant culture you would need to use:

using System.Globalization;

// [ ... ]
float f = 3.1415f;
string s = f.ToString(CultureInfo.InvariantCulture)); // "3.1415"

string numString = "3.1415";
float num = float.Parse(numString, CultureInfo.InvariantCulture));

If you want to use a specific culture you would have to use

CultureInfo.CreateSpecificCulture("en-GB")
// or
CultureInfo.CreateSpecificCulture("de-DE")
// or ...

inplace of the CultureInfo.InvariantCulture. If you don’t specify a culture the system settings are used. Even i’m german and my settings are set to getman, i’ve manually set the decimal point to the dot in my system settings as I’m used to use the dot. Though if you want your code / game work internationally you have to decide if you want to use the users local culture or if you want to force an invariant culture. It’s generally adviced if you store something to a file or format data that is exchanged with others you should use the invariant culture so reading and writeing works exactly the same everywhere. User input usually should be culture dependent so users can enter numbers the way they are used to.

Though i agree with the others that you usually should use TryParse in almost all cases. I would only use Parse if i know where the text comes from. So never for user input.

I think what you entered was not a real float. It might just have contained a space or a letter. A solution to this problem might be:

 public InputField mass;
 float val;

 if(float.TryParse(mass.text, out val))
 {
     //Here you can put code that will only run if the text is a valid number
 }
else{
    Debug.Log("Text is not a valid number");
    //You can replace this with whatever code you want to run if the text is not a number, perhaps show some text telling the player that they have insert a incorrect string
}

Good luck!