Search Unity

FormatException: Input string was not in the correct format

Discussion in 'Scripting' started by MG, Sep 24, 2017.

  1. MG

    MG

    Joined:
    Nov 10, 2012
    Posts:
    190
    I'm getting the error: FormatException: Input string was not in the correct format

    Code (CSharp):
    1. FormatException: Input string was not in the correct format
    2. System.UInt64.Parse (System.String s, NumberStyles style, IFormatProvider provider) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/UInt64.cs:351)
    3. System.UInt64.Parse (System.String s) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/UInt64.cs:99)
    4. System.Convert.ToUInt64 (System.String value) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Convert.cs:2424)
    5. backToMenu.ChangeScene () (at Assets/backToMenu.cs:25)

    The thing I want the code to do is, when I change Scene, I want it to store the current amount of money as string, then convert it to a ulong, then add the CurrentMoney and Gathered money together and then convert it to a string for then save it as a playerPrefs.

    But It doesn't work, why?
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System;
    5.  
    6.  
    7.  
    8. public class backToMenu : MonoBehaviour {
    9.  
    10.     // Use this for initialization
    11.     void Start() {
    12.  
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update() {
    17.  
    18.     }
    19.  
    20.     public void ChangeScene()
    21.     {
    22.        
    23.  
    24.         string CurrentMoneyAsString = PlayerPrefs.GetString("Money");
    25.         ulong CurrentMoneyAsUlong = Convert.ToUInt64(CurrentMoneyAsString);
    26.         ulong TotalMoney = CurrentMoneyAsUlong + GameObject.Find("Player").GetComponent<PlayerProperties>().Money;
    27.         string TotalMoneyAsString = TotalMoney.ToString();
    28.        
    29.  
    30.         PlayerPrefs.SetString("Money", TotalMoneyAsString);
    31.         Application.LoadLevel("Main Menu");
    32.     }
    33.  
    34.  
    35.    
    36.  
    37. }
    38.  
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    The exception and its stack trace tells you about it:

    You read it the following, from bottom to top if you don't have an instant idea:

    Everything (that you would care about) starts here:
    That's your script backToMenu and your method ChangeScene.
    It tells you the error is related to line 25, if you check that line, you'll find that you try to convert a string to a ulong (uint64).

    This is also reflected in the stack trace, by the next line, which is exactly the method you call into:
    And so on.

    The exception basically says that the string you've passed to the converter does not match the expected format for the target type of the conversion.

    This could have several reasons, you may want to debug this or print the value to the console. it could be that the value loaded from PlayerPrefs is null or empty, or has a completely different format.
     
  3. MG

    MG

    Joined:
    Nov 10, 2012
    Posts:
    190
    I'm sorry Suddoha.

    But as always you were right. The playerPrefs did apperently not contain something that would be valid. I set the playerpref string as "0" and it worked out.

    Thanks again!
     
  4. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    To handle this situation more elegantly, the getter of the PlayerPrefs also allow to return a default value, if the entry does not yet exist:

    Code (csharp):
    1. ... = PlayerPrefs.GetString(yourKey, "0");
    This basically says, get the value for yourKey and if it does not exist, just return the default value I passed, in this example "0".