Search Unity

Help with the incomprehensible "FormatException" error.

Discussion in 'Scripting' started by carlospicomonte, Oct 24, 2021.

  1. carlospicomonte

    carlospicomonte

    Joined:
    Jan 8, 2019
    Posts:
    26
    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();

    }
     
  2. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,092
    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.
     
    carlospicomonte and Bunny83 like this.
  3. carlospicomonte

    carlospicomonte

    Joined:
    Jan 8, 2019
    Posts:
    26
    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).
     
  4. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,092
    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.
     
    Last edited: Oct 24, 2021
    carlospicomonte and Kurt-Dekker like this.
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,674
    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:

    '123
    '


    and this starts with a space:

    ' 123'


    Every single character must be correct in computers.
     
    carlospicomonte likes this.
  6. carlospicomonte

    carlospicomonte

    Joined:
    Jan 8, 2019
    Posts:
    26
    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 :)