- Home /
Returning from Coroutine stops the Update() method.
I've been creating a menu state system with a login at the start for an app I've been working on, and for some reason after the callback in the coroutine, my update() seems to stop. I'm not entirely sure why? Am I missing something? Below is the class that's doing this:
public class MainUIControls : MonoBehaviour {
private EventSystem system;
// Menu states.
//private enum menuStates { LOGIN, USER_MENU, AVATAR_MENU, SERVER_MENU, SERVER_LIST, CREATE_SERVER };
private const int LOGIN=0, USER_MENU=1, AVATAR_MENU=2, SERVER_MENU=3, SERVER_LIST=4, CREATE_SERVER=5;
private int currentMenuState;
// User information
private string username;
private bool userLogInProgress = false;
private bool userLoggedIn = false;
// Menu Login Booleans
private bool usernameSelected=true;
private bool passwordSelected=false;
private bool loginSelected=false;
// User Menu Booleans
private bool userAvatarLoaded = false;
// We need to keep this pointer so we can bring it back if needed.
private GameObject loginFields;
private GameObject userUI;
// Method that runs concurrently in the login menu.
// Sends a request to script to login to system.
IEnumerator LoginToSystemCoroutine(Action<WWW> loginCallback) {
this.username = GameObject.Find ("UsernameField").GetComponent<InputField> ().text;
WWWForm theform = new WWWForm ();
theform.AddField ("f", "conv_request");
theform.AddField ("s", "app_login");
theform.AddField ("username", this.username);
theform.AddField("password", GameObject.Find ("PasswordField").GetComponent<InputField> ().text);
WWW www = new WWW("http://xxxxxx/xxx.php", theform);
yield return www;
loginCallback (www);
}
void LoginToSystemCB(WWW www) {
if ((!string.IsNullOrEmpty (www.error))) {
print ("Error downloading!");
} else {
int server_return = int.Parse(www.text);
Debug.Log (www.text);
if(server_return == 1) {
userLoggedIn = true;
// Disable loginfields
GameObject.Find ("LoginStatus").GetComponent<Text>().text = "Login Successful!";
loginFields.SetActive(false);
//Enable UserMenu
userUI.SetActive(true);
ChangeGameState(USER_MENU);
GameObject.Find ("LoggedInAs").GetComponent<Text> ().text = username;
} else {
GameObject.Find ("LoginStatus").GetComponent<Text>().text = "Invalid Username/Password";
}
}
}
// State method that controls login menu functions
void MenuLoginControls() {
if (Input.GetMouseButton (0)) {
GameObject.Find ("UsernameField").GetComponent<InputField> ().Select ();
usernameSelected = true;
passwordSelected = false;
loginSelected = false;
}
if (usernameSelected) {
if (Input.GetKeyDown (KeyCode.Return) || Input.GetKeyDown (KeyCode.Tab)) {
GameObject.Find ("PasswordField").GetComponent<InputField> ().Select ();
usernameSelected = false;
passwordSelected = true;
}
} else if (passwordSelected) {
if (Input.GetKeyDown (KeyCode.Return) || Input.GetKeyDown (KeyCode.Tab)) {
GameObject.Find ("LoginButton").GetComponent<Button>().Select();
passwordSelected = false;
loginSelected = true;
}
} else if(loginSelected) {
if (Input.GetKeyDown (KeyCode.Return) || Input.GetKeyDown (KeyCode.Tab)) {
StartCoroutine(LoginToSystemCoroutine(LoginToSystemCB));
}
} else {
GameObject.Find ("UsernameField").GetComponent<InputField> ().Select ();
usernameSelected = true;
passwordSelected = false;
loginSelected = false;
}
}
void MenuUserUI() {
Debug.Log ("MenuUserUI()");
}
// Use this for initialization
void Start () {
// Current Menu State is Login
ChangeGameState (LOGIN);
system = EventSystem.current;
loginFields = GameObject.Find ("Login");
userUI = GameObject.Find ("LoggedInAs");
GameObject.Find ("UsernameField").GetComponent<InputField> ().Select ();
// Set UserUI disabled at first
userUI.SetActive (false);
}
void ChangeGameState(int state) {
currentMenuState = state;
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.F12)) {
//OVRManager.display.RecenterPose();
}
switch(currentMenuState) {
case LOGIN:
MenuLoginControls();
break;
case USER_MENU:
MenuUserUI();
break;
}
}
}
Comment
Are you getting any errors?
[ed: deleted answer as bonfireBoy pointed out.]
Everytime you use SetActive(true) do a little debug to see if the object is actually beco$$anonymous$$g reactivated.
Your update is probably still going it's just not reaching any of the states you have specified in there. To me this looks like it could fall apart at any moment. You're starting coroutines from the Update function which is a recipe for disaster.