- Home /
Input string was not in the correct format
Anybody know why this error is happening? Input string was not in the correct format System.Int32.Parse (System.String s) This is my script.Something that i notice that on line 87.The
PlayerPrefs.SetInt("xp",int.Parse(w.text));
is not working.I debug.log it.The w.text is working fine but SetInt is not.Please help.Thanks
public class InternetCalls : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void DoLogin(string User,string Pass)
{
WWWForm www = new WWWForm ();
www.AddField ("user", User);
www.AddField ("password", Pass);
WWW w = new WWW ("http://reactivestudios.comuv.com/login.php", www);
StartCoroutine (Login(w, User));
GetXp (User);
}
public void DoRegister(string User,string Pass, string Name)
{
WWWForm www = new WWWForm ();
www.AddField ("user", User);
www.AddField ("password", Pass);
www.AddField ("name", Name);
WWW w = new WWW ("http://reactivestudios.comuv.com/register.php", www);
StartCoroutine (Reg(w));
}
IEnumerator Reg (WWW w)
{
yield return w;
if(w.error == null)
{
//PlayerPrefs.SetString("name", User);
Debug.Log ("Working");
Debug.Log (w.text.ToString());
}
}
IEnumerator Login (WWW w, string User)
{
yield return w;
if(w.error == null)
{
if(w.text == "login-SUCCESS")
{
PlayerPrefs.SetString("name",User);
Debug.Log ("Working");
Application.LoadLevel(1);
}
}
else
{
Debug.Log ("Error:" + w.error.ToString());
}
}
public void GetXp(string User)
{
WWWForm www = new WWWForm ();
www.AddField ("user", User);
WWW w = new WWW ("www.reactivestudios.comuv.com/Exp.php",www);
StartCoroutine (Xp (w));
Debug.Log ("Got Exp");
}
IEnumerator Xp(WWW w)
{
yield return w;
if(w.error == null)
{
//PlayerPrefs.SetString("name", User);
Debug.Log ("Getting Xp for Stats");
Debug.Log (w.text);
Debug.Log ("Working Stats");
PlayerPrefs.SetInt("xp",int.Parse(w.text));
Debug.Log ("Get xp Worked Stats");
}
else
{
Debug.Log ("Error: " + w.error.ToString());
}
}
}
For future reference only use the title unexpected error when you get an 'unexpected error'. Its a specific error type.
You don't give us much information about the error, but int.Parse() raises a FormatException. So it is likely that your problem is in the int.Parse() (and therefore the string), and not in the SetString() method. To start with, break everything down and use int.TryParse():
int i;
if (TryParse(w.text, out i) {
PlayerPrefs.SetInt("xp",i);
}
else {
Debug.Log("Parsing failed");
Debug.Log(w.text+", "+w.text.Length);
}
If you get a 'Parsing failed' message, compare the visible 'w.text' string to the length of the string. If the number of visible characters doesn't match the output Length, you likely have some non-printable characters in your string.
full error:FormatException: Input string was not in the correct format System.Int32.Parse (System.String s) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/Int32.cs:629) InternetCalls+c__Iterator2.$$anonymous$$oveNext () (at Assets/Scripts/Level/InternetCalls.cs:87)
Yep i got Parsing failed. Changed script to this.
Debug.Log (w.text);
Debug.Log ("Working Stats");
int i;
if (int.TryParse(w.text, out i))
{
Debug.Log ("Getting xp Worked Stats");
PlayerPrefs.SetInt("xp",i);
Debug.Log ("Get xp Worked Stats");
}
else
{
Debug.Log("Parsing failed");
Debug.Log(w.text+", "+w.text.Length);
}
}
Answer by NITINIVS · Jul 14, 2014 at 06:04 AM
www.text not returning value in integer format
So ,please check it & instead of using int.Parse use int.TryParse
Eventhough i set my exp to 100000 on webhost but i get the length of 169
IEnumerator Xp(WWW w) { yield return w; if (w.error == null) {
//Debug.Log (w.text);
//Debug.Log ("Working Stats");
//PlayerPrefs.SetInt("xp",int.Parse(w.text));
//Debug.Log ("Get xp Worked Stats");
Debug.Log (w.text);
Debug.Log ("Working Stats");
int i;
if (int.TryParse(w.text, out i))
{
Debug.Log ("Getting xp Worked Stats");
PlayerPrefs.SetInt("xp",i);
Debug.Log ("Get xp Worked Stats");
}
else
{
Debug.Log("Parsing failed");
Debug.Log(w.text.Length);
}
}
Answer by JScotty · May 12, 2015 at 12:52 PM
you need to TryParse, and when it is true let it Parse so like:
bool Result;
int number;
Result = int.TryParse(txt, out number);
if(result){
//parse
}
Your answer
Follow this Question
Related Questions
Parser error for code... 1 Answer
Switching between two weapons 1 Answer
Unexpected error 1 Answer
Quill18's Tutorial Scripts: Unexpected Symbols 1 Answer
String Multiplying 1 Answer