I’m trying to create a register/ login system for my game. I’m following a tutorial. Everything had been working great, until I tried to implement using a User class create from JSON instead of just grabbing the values directly from the JSON array (commented section towards end of code). I keep getting the error mentioned in the title. Here is my code snippet:
[Serializable]
public class User{
public bool success;
public string error;
public string email;
}
private IEnumerator RequestLogin(){
string email = textInputEmail.text;
string password = textInputPassword.text;
form = new WWWForm ();
form.AddField ("email", email);
form.AddField ("password", password);
WWW w = new WWW ("http://localhost/action_login.php",form);
yield return w;
if (string.IsNullOrEmpty (w.error)) {
User user = JsonUtility.FromJson<User> (w.text);
if (user.success == true) {
if (user.error != "") {
textFeedback.text = user.error;
} else {
textFeedback.text = "Login successful.";
}
} else {
textFeedback.text = "An error occured.";
}
}
/*
if(w.text.Contains("invalid email or password")){
textFeedback.text = "Invalid email or password.";
}else{
textFeedback.text = "Login successful.";
}
} else {
//error
textFeedback.text = "An error occured.";
}
*/
}
Thank your for the reply. I haven’t programmed in a while and completely forgot the power of printing debug statements to the console. I printed w.text to the console, and it turned out that I had some test echo statements in my PHP that were being sent via the JSON object. Therefore the JSON parser couldn’t understand it. I removed them and now it is only sending the proper values, and all is working well.