- Home /
Variable is always false...
Hello, so I have this code which works except no matter where I put login = true, it is always false. Why is this? It used to work and I changed something and now it no longer works. It seems that it is checking the variable before the coroutine gets to the point of setting the variable. Any help would be appreciated.
Code:
using UnityEngine;
using System.Collections;
public class Login : MonoBehaviour
{
public Texture LoginBackground;
public Texture2D stylebackground;
public GUIStyle LoginStyle;
public GUIStyle LoginTextBox;
public GUIStyle LoginButton;
public string Username;
public string Password;
public string EncPassword;
public float transparent;
private string url;
public WWW w;
public WWWForm loginform;
public bool doWindow0 = false;
public string formText="";
public bool login;
// Use this for initialization
void Start ()
{
LoginStyle.fontSize = 72;
LoginStyle.alignment = TextAnchor.MiddleCenter;
LoginTextBox.fontSize = 20;
LoginTextBox.alignment = TextAnchor.MiddleCenter;
LoginTextBox.normal.background = stylebackground;
LoginButton.fontSize = 30;
LoginButton.alignment = TextAnchor.MiddleCenter;
loginform = new WWWForm ();
}
// Update is called once per frame
void Update ()
{
}
void OnGUI ()
{
GUI.backgroundColor = Color.black;
GUI.DrawTexture (new Rect (0, 0, Screen.width, Screen.height), LoginBackground, ScaleMode.StretchToFill, false, 0.0f);
GUI.Label (new Rect (Screen.width / 2 - 250, Screen.height / 2 - 250, 500, 250), "Username:", LoginStyle);
Username = GUI.TextField (new Rect (Screen.width / 2 - 250, Screen.height / 2 - 80, 500, 50), Username, 10, LoginTextBox);
GUI.Label (new Rect (Screen.width / 2 - 250, Screen.height / 2 - 50, 500, 250), "Password:", LoginStyle);
Password = GUI.TextField (new Rect (Screen.width / 2 - 250, Screen.height / 2 + 120, 500, 50), Password, 10, LoginTextBox);
if (GUI.Button (new Rect (Screen.width / 2 - 150, Screen.height / 2 + 200, 300, 50), "Login:", LoginButton)) {
//Encrypt
EncPassword = EncryptData.Encrypt(Username,Password);
//CheckLogin
StartCoroutine(CheckLogin());
doWindow0 = true;
}
if (doWindow0 == true) {
GUI.Window (0, new Rect (Screen.width / 2 - 700, Screen.height / 2 - 100, 400, 120), DoMyWindow, "Notice:");
}
}
void DoMyWindow (int windowID)
{
GUI.Label (new Rect (10, 20, 800, 40), "Login success. Click the close button to continue to the character selection screen.");
if (GUI.Button (new Rect (10, 80, 100, 20), "Close")) {
print("login is " + login);
if(login==true)
Application.LoadLevel ("CharacterSelect");
doWindow0 = false;
}
}
IEnumerator CheckLogin ()
{
url = "http://redlightlife.tk/scripts/checklogin.php?username=" + WWW.EscapeURL (Username) + "&password=" + WWW.EscapeURL (EncPassword);
w = new WWW (url);
yield return w;
if (w.error != null) {
print (w.error);
}
if (w.error == null) {
formText = w.text;
w.Dispose ();
formText.Trim();
if (formText == "true")
{
login = true;
}
else if(formText == "false")
{
login = false;
}
StopAllCoroutines();
}
}
}
Answer by golden pouria · Feb 07, 2013 at 04:44 AM
There are different possiblities:
1- In the line number 164 you defined:
formText = w.text;
which is the actual value that you compare in your IF statements in the lines 169 and 177. The value of formText defines whether or not the "login" is "True" or "False". Please note that in lines 169 and 177 you are comparing a string value, and not a Boolean value. So, it could be that you get "True", instead of "true" and you have to see if that makes any difference.
I would suggest that you debug your code by putting the following code at line 168, to see what is the real value in different cases. It will print whatever that the value of the formText is:
print(formText);
Then you will see it and you can find out why it doesn't return what you want in the IF statements.
PLEASE NOTE:
in line 161, where you check for
if (w.error == null)
if the statment returns false, then you would never get to any of the IF statemtnes in the above. So I would also put a
print (w.error);
too before 161 to make sure.
Have fun!
Hi thanks for the reply. First, I'm not sure what line numbers you are referring to as they are not in the code. Also, if w.error is not null it is already set to print the error. Now I have placed sever print(formText) in a few places. Anytime inside of the co-routine it returns a value, anywhere else, it returns a blank. It is like it's reading the value before it gets set in the co-routine.
By line numbers, I mean in your post, when you post your question, this website automatically puts a line number next to each line of code (5,10,15,...)
Also if what do you mean by "Anytime inside of the co-routine it returns a value". What value does it return? You are checking for 2 string values "true" and "false". anything other than these 2 values don't work for you. In other words, does it return the word "true" (of type string)? If yes, then you should expect the "login" to be true. Other than that it will be always false.
Hope it works.
well when I check formText it is true and login is also true. Only when in the routine though. Everywhere else they're empty. Also the line numbers you mentioned don't exist. It only goes up to 91.
Hi, so I added the print error line and it returned with Null. At the line you line you specified I added the print formText and it returned ( true). The problem is, there is a space. However I am not sure why since I called the trim function before hand. Up in the OnGUI, I added print(formText) again right after coroutine and it returned false.