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();

		}

	}

}

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!