pomelo-unitychat-socket icon indicating copy to clipboard operation
pomelo-unitychat-socket copied to clipboard

Change scene error: get_loadedLevel can only be called from the main thread.

Open gclsoft opened this issue 10 years ago • 1 comments

Press login and can't change scene:

get_loadedLevel can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.

gclsoft avatar Jul 12 '14 19:07 gclsoft

Here is what I did to fix the issue. Instead of calling it Application.loadscene in Entry (which is obsolete by the way, use SceneManager instead), do it in update instead. Just have a boolean that you can toggle. Example:

void Update(){
  ....
  if (loadscene) {
    //SceneManager.LoadScene("chat");
    Application.LoadLevel(Application.loadedLevel + 1);
    loadscene = false;
  }
}

void Entry(){
  ....
  pc.request("connector.entryHandler.enter", userMessage, (data)=>{
    users = data;
    loadscene = true;
  });
  ....
}

tqtran7 avatar Feb 09 '16 11:02 tqtran7