Format exception: Input string was not in the correct format

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+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:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

namespace UnityStandardAssets.Vehicles.Car
{
    public class SaveData : MonoBehaviour {

        public InputField InputUsername;
        public InputField InputAge;
        public Dropdown InputGender;
        public Text err;

        public static string name;

        string CreateUserUrl = "http://localhost/MyCarGame/InsertUser.php";
        public static bool flag = false;

        public GameObject manager;



        public void CreateUser()
        {
            StartCoroutine(usr());

        }

        public void start_game()
        {

            manager.GetComponent<Settings>().ChangeCams(6);
          
        }
        private IEnumerator usr()
        {
            string gender;
            if (InputGender.value == 0)
            {
                gender = "Male";
            }
            else
            {
                gender = "Female";
            }
            if (InputUsername.text != "" && InputAge.text != "")
            {
                flag = true;
              //  print("User saved");
                WWWForm form = new WWWForm();
                form.AddField("UsernamePost", InputUsername.text);
                form.AddField("AgePost", InputAge.text);
                form.AddField("GenderPost", gender);

                name = InputUsername.text;

                WWW www = new WWW(CreateUserUrl, form);

                 yield return www;
                string[] temp = www.text.Split(",".ToCharArray());

                int value1 = System.Int32.Parse(temp[0]);

                if (value1 == 2)
                {
                    print("User Signed Up");
                    start_game();
                    err.enabled = false;
                }
                else
                {
                   // print("User Exists");
                    err.enabled = true;
                }

            }


        }



    }
}

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 “1,2,3”, the first item in the array would be “1”, which can’t be turned into a number.

@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.

1 Like