• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Sketarius · Feb 03, 2015 at 11:33 AM · coroutinewwwcoroutineswwwformgamestate

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
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image _MGB_ · Feb 03, 2015 at 01:50 PM 0
Share

Are you getting any errors?

[ed: deleted answer as bonfireBoy pointed out.]

avatar image meat5000 ♦ · Feb 03, 2015 at 02:01 PM 0
Share

Everytime you use SetActive(true) do a little debug to see if the object is actually beco$$anonymous$$g reactivated.

avatar image smoggach · Feb 03, 2015 at 02:35 PM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Coroutine for WWW call not resuming 0 Answers

Coroutine stopping before it is complete 1 Answer

Question about coroutines 2 Answers

NullReferenceException in StartCoroutine 1 Answer

If the While Condition of a Coroutine is false is the coroutine halted? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges