- Home /
IF doesn't work
I made a js file to login on my game, but the IF statement doesn't work. And in the debug window i clearly see 'error'
function Login() {
var form = new WWWForm(); //here you create a new form connection
form.AddField( "myform_hash", hash ); //add your hash code to the field myform_hash, check that this variable name is the same as in PHP file
form.AddField( "myform_nick", formNick );
form.AddField( "myform_pass", formPassword );
var w = WWW("http://sociallife.woelmuis.nl/login.php", form); //here we create a var called 'w' and we sync with our URL and the form
yield w; //we wait for the form to check the PHP file, so our game dont just hang
if (w.error != null) {
print(w.error); //if there is an error, tell us
} else {
answer = w.text;
answer = answer.Replace("<div align=\"center\">","");
answer = answer.Replace(" ", "");
status = answer;
Debug.Log(answer); //here we return the data our PHP told us
if(answer == "error")
{
Debug.Log("loggedin");
}
w.Dispose(); //clear our form in game
}
formNick = ""; //just clean our variables
formPassword = "";
}
Hi there. I think you will have to post more information. What is it about the "if" that doesn't work? What is the nature of the error?
It doesn't write any 'loggedin' in the debug window. Even the other message in my window clearly says 'error'
Answer by perchik · May 09, 2012 at 04:59 AM
So inside the first Else clause, answer is getting set to "error". You then print out answer, and it tells you it is indeed "error". Then you check to see if answer=="error" and it doesn't go into that If statement.
My guess, considering that you are using PHP is that there's some kind of whitespace issue. I see that you are deleting spaces, but that might not mean you're catching all whitespace. Try Debug.Log("|||" + answer + "|||" ) if it doesn't say |||error||| then theres some whitespace you need to trim.
If it does come back as |||error||| then we can go from there.
In the debug line it says |||error . not the last ||| . In the 'extra information' space of the debug window it tells me this "
|||succes varsc_project=7295936; varsc_invisible=1; varsc_security="ee708cde";
||| UnityEngine.Debug:Log(Object) $:$$anonymous$$oveNext() (at Assets/Scripts/LoginGUI.js:73)"
So you see where the problem is now?
if(answer=="error") comes back false because answer is "error. not the last" which does not equal "error"
Good luck handling that!
Answer by -hiTo- · May 01, 2012 at 12:49 PM
I don't really understand your problem... If you get an error message from your WWW-object, it should say "Error" in your debug window. You yourself has made it so:
if (w.error != null) {
print(w.error); //if there is an error, tell us
}
This just tells me there is something wrong along the way. Either you're giving the wrong address or a connection could not be made, or something along those lines. But we will never know if you don't tell us what the error says and on which line, etc.
No, it is a login script. If i use the wrong login info it shows error in the debug window, but when i fill in correct info it properly shows 'success' but when I use if to check what it returned it doesn't work.
Are you telling me that Debug.Log(answer); prints "success" and that the following if-statement doesn't fire? That would be because the if-statement will only fire if 'answer' is "error", not "success". Otherwise I'm still having trouble understanding your problem. You need to tell us WHICH if-statement does not work, and WHAT prints "success" and WHEN you want this if-statement to fire.
Answer by mrldev · May 09, 2012 at 06:14 PM
What if it can process the if directive, jumps to the else directive and finds it cannot do what it is supposed to. It would not return anything. Add another step. Perhaps turn the else into an else if and then add the else for if both fail.