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