Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Format exception: Input string was not in the correct format

Discussion in 'Scripting' started by MofiAdeyemi, Oct 26, 2017.

  1. MofiAdeyemi

    MofiAdeyemi

    Joined:
    Oct 25, 2017
    Posts:
    1
    Hi I need help

    Anytime I click Go after entering the player's information, it comes up with "Format exception: Input string was not in the correct format"
    This is the full message for the error.

    FormatException: Input string was not in the correct format
    System.Int32.Parse (System.String s) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Int32.cs:629)
    UnityStandardAssets.Vehicles.Car.SaveData+<usr>c__Iterator0.MoveNext () (at Assets/Standard Assets/Vehicles/Car/Scripts/SaveData.cs:62)
    UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)

    This is the code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. namespace UnityStandardAssets.Vehicles.Car
    6. {
    7.     public class SaveData : MonoBehaviour {
    8.  
    9.         public InputField InputUsername;
    10.         public InputField InputAge;
    11.         public Dropdown InputGender;
    12.         public Text err;
    13.  
    14.         public static string name;
    15.  
    16.         string CreateUserUrl = "http://localhost/MyCarGame/InsertUser.php";
    17.         public static bool flag = false;
    18.  
    19.         public GameObject manager;
    20.  
    21.  
    22.  
    23.         public void CreateUser()
    24.         {
    25.             StartCoroutine(usr());
    26.  
    27.         }
    28.  
    29.         public void start_game()
    30.         {
    31.  
    32.             manager.GetComponent<Settings>().ChangeCams(6);
    33.          
    34.         }
    35.         private IEnumerator usr()
    36.         {
    37.             string gender;
    38.             if (InputGender.value == 0)
    39.             {
    40.                 gender = "Male";
    41.             }
    42.             else
    43.             {
    44.                 gender = "Female";
    45.             }
    46.             if (InputUsername.text != "" && InputAge.text != "")
    47.             {
    48.                 flag = true;
    49.               //  print("User saved");
    50.                 WWWForm form = new WWWForm();
    51.                 form.AddField("UsernamePost", InputUsername.text);
    52.                 form.AddField("AgePost", InputAge.text);
    53.                 form.AddField("GenderPost", gender);
    54.  
    55.                 name = InputUsername.text;
    56.  
    57.                 WWW www = new WWW(CreateUserUrl, form);
    58.  
    59.                  yield return www;
    60.                 string[] temp = www.text.Split(",".ToCharArray());
    61.  
    62.                 int value1 = System.Int32.Parse(temp[0]);
    63.  
    64.                 if (value1 == 2)
    65.                 {
    66.                     print("User Signed Up");
    67.                     start_game();
    68.                     err.enabled = false;
    69.                 }
    70.                 else
    71.                 {
    72.                    // print("User Exists");
    73.                     err.enabled = true;
    74.                 }
    75.  
    76.             }
    77.  
    78.  
    79.         }
    80.  
    81.  
    82.  
    83.     }
    84. }
    85.  
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    The message is fairly straightforward: what you're feeding into int.Parse() is not something that can be converted into a number. Since you're parsing some input text from a WWW request, we'd have to see what the input text is to give specific help (that is, what's contained in www.text?)

    90% of the time when you're parsing a string.split result, the problem is that you may have an empty item in the list. If your text says ",1,2,3", then .split()[0] will be an empty string. Since this is coming from the web, there might also be some HTML stuff surrounding it. So like "<body>1,2,3</body>", the first item in the array would be "<body>1", which can't be turned into a number.
     
  3. iamvideep

    iamvideep

    Joined:
    Oct 27, 2017
    Posts:
    118
    @StarManta is right. Maybe your string that you are getting is empty or cannot be parsed to an integer. Always use if then checks and/or try catch statements to avoid such errors.
    try and check if the string can be parsed to integer. if error shows up, catch it and exit the function or skip the path.
     
    greatastrocow likes this.