Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

LoadScene not work inside Async Method.

Discussion in 'Scripting' started by andresblend7, Mar 25, 2022.

  1. andresblend7

    andresblend7

    Joined:
    Apr 29, 2018
    Posts:
    3
    Hello, I am currently developing a game that connects with a WebSocket in Node.js, I frequently use " SceneManager.LoadScene("GamePlay")" to change the scene and it works for me without problem.

    However when I execute this command inside an asynchronous method nothing happens, it doesn't generate any error but it doesn't change the scene either, I tried to place it in a coroutine to use "SceneManager.LoadSceneAsync" but it doesn't work either.

    When debugging in visual studio it just doesn't do anything, it doesn't generate any errors.

    Method to get the websocket message:

    Code (CSharp):
    1.     private void ReadFinish(IAsyncResult arr)
    2.     {
    3.         this.readingData = false;
    4.         int dataSize = stream.EndRead(arr);
    5.         string servermessage = Encoding.UTF8.GetString(data, 0, dataSize);
    6.  
    7.         Debug.Log("Message From Server: " + servermessage);
    8.  
    9.         this.ListenServerCommands(servermessage);
    10.     }

    Method to change scene:

    Code (CSharp):
    1.  private void ListenServerCommands(string message)
    2.     {
    3.  
    4.         var resultJson = JsonUtility.FromJson<NetWorkingMessage>(message);
    5.         var messageType = Type.GetType(resultJson.MessageType);
    6.  
    7.         if (messageType == typeof(ConnectionStatus))
    8.         {
    9.  
    10.             var response = JsonUtility.FromJson<ConnectionStatus>(resultJson.Message);
    11.             if (response.code == "OK")
    12.             {
    13.                 connectionDone = true;
    14.                 Debug.Log("Conn OK");
    15.             }
    16.  
    17.         }
    18.         else if (messageType == typeof(RoomCreationResponse))
    19.         {
    20.  
    21.             var response = JsonUtility.FromJson<RoomCreationResponse>(resultJson.Message);
    22.  
    23.            // SWITCH SCENE NOT WORK
    24.             SceneManager.LoadScene("GamePlay");
    25.  
    26.             this.callBackCLientEvent(response);
    27.  
    28.         }
    29.  
    30.     }
     
  2. nilsdr

    nilsdr

    Joined:
    Oct 24, 2017
    Posts:
    374
    Chances are the function is executing on a different thread which won't work. A common pattern is to add an action to a queue from the other thread, and then execute it in the update loop
     
    Kurt-Dekker and Bunny83 like this.
  3. andresblend7

    andresblend7

    Joined:
    Apr 29, 2018
    Posts:
    3
    Thanks a lot !!!!! <3

    My solution was use a bool variable, in Update() method check if it changes and works !!!!
     
    Last edited: Mar 25, 2022
    nilsdr likes this.