Help with the incomprehensible "FormatException" error.

Someone find the reason why if it works fine:

a = Int32.Parse (b.text);
total1.text = a.ToString ();

but nevertheless Unity throws me this error (“FormatException: Input string was not in a correct format.”) here?

c = Int32.Parse(d.text);
total2.text = c.ToString();

my code:

public int a;
public int c;

public Text b;
public Text d;

public Text total1;
public Text total2;

void Start()
{
b.text = PlayerPrefs(“abc”, abc2.text);
d.text = PlayerPrefs(“bcd”, abc3.text);
}

void Update()
{
a = Int32.Parse(b.text);
total1.text = a.ToString();

c = Int32.Parse(d.text);
total2.text = c.ToString();

}

Use Code tags on the forum, it makes code a lot easier to read.
A format exception on a = Int32.Parse(b.text); would indicate that the b.text does not contain a valid value.
The text should only be numeric then. Nothing else. If you have a text “Score: 3896” and use the int.Parse(score.text) it would fail. Because it contains "Score: " in it. Which is not valid for parsing.

There is also int.TryParse which returns a boolean whether it was able to convert the string into an integer value.

2 Likes

It’s the strange thing. Everything is done exactly the same, one line works and the other does not, they only contain numbers, not text. In another scene I save the “PlayerPrefs” in an “InputField” where you can only insert numbers (inspector / content type / integer).

Well like Kurt always says, sprinkle it with debug logs.

You store and reload your string value. You convert from and to integers / strings.
When you get an exception that your conversion is not valid. It’s not valid. It’s not strange, it is an error in your code somewhere.
I don’t know what PlayerPrefs("key", textComponent.text); does in your code but it sure is your own method. Is it loading or saving a value?

Try using int.TryParse and when TryParse returns false, output a Debug.Log($"'{textComponent.text}' is not an integer value");
Maybe the input string is empty? you’re not checking that either. string.IsNullOrEmpty(textComponent.text)

What if the value you thought you stored in PlayerPrefs is actually something completely else.
Or you’ve got another component in the scene, overwriting that PlayerPref value.
Output the Value when you’re saving/loading it.

2 Likes

Also, note how MaskedMouse cleverly put little single-quote tick marks on either side of the variable. This will reveal otherwise-invisible characters such as space or newline. This is highly likely to be related to solving your problem.

This would be correct to get the value 123 out of a string:

'123'

These would be wrong:

This has a newline:

'```

and this starts with a space:

```' 123'```

Every single character must be correct in computers.
1 Like

Everything worked perfectly when declaring float instead of int … Why? Damn, I was using a decimal value in another scene. The truth is that you are always, always wrong somewhere, and that you have to be very attentive to everything you do. Thank you both very much, I was stuck and confused … I will learn, I promise :slight_smile: