LoadScene() working on build but not in Play Mode inside ReceivedMatchmakerMatched callback

Hi everyone, this is my first time working with Nakama, all works well except when i try to load a new scene. If i make a build of my project, the scene loads correctly, but when i try to load it from the Unity Playmode it doesn’t work.

If somebody can help me it will be great.

Greetings.

@degreerichi Welcome!

It would be useful to know exactly what goes wrong: do you have an error, stack trace, crash log, a description of the behaviour you see (crash/freeze) etc? We want to help but “it doesn’t work” doesn’t give us much to work with.

Edit: Not enough information to diagnose the problem, will consider this conversation closed. Come back with more information if you need further help.

@zyro Hi, I have the same problem, I want to load a scene after received matchmaker matched, it worked in build mode, but not work in editor play mode. the code is:

void LobbyReceivedMatchmakerMatched(IMatchmakerMatched matched) {
    Debug.LogFormat("Received: {0}", matched);
    var opponents = string.Join(",\n  ", matched.Users);
    Debug.LogFormat("Matched opponents: [{0}]", opponents);
    try {
        SceneManager.LoadScene("BattleScene");
    }
    catch (Exception e) {
        Debug.Log(e.ToString());
    }
}

exception message is

UnityEngine.UnityException: LoadSceneAsyncNameIndexInternal 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.
at (wrapper managed-to-native) UnityEngine.SceneManagement.SceneManager.LoadSceneAsyncNameIndexInternal_Injected(string,int,UnityEngine.SceneManagement.LoadSceneParameters&,bool)
at UnityEngine.SceneManagement.SceneManager.LoadSceneAsyncNameIndexInternal (System.String sceneName, System.Int32 sceneBuildIndex, UnityEngine.SceneManagement.LoadSceneParameters parameters, System.Boolean mustCompleteNextFrame) [0x00000] in <0df6749ea7eb43f58475647fabed0313>:0
at UnityEngine.SceneManagement.SceneManager.LoadScene (System.String sceneName, UnityEngine.SceneManagement.LoadSceneParameters parameters) [0x00001] in <0df6749ea7eb43f58475647fabed0313>:0
at UnityEngine.SceneManagement.SceneManager.LoadScene (System.String sceneName) [0x00009] in <0df6749ea7eb43f58475647fabed0313>:0
at Lobby.LobbyReceivedMatchmakerMatched (Nakama.IMatchmakerMatched matched) [0x0003d] in E:\Projects\Games\Unity3D\GameOfWizards\Assets\Scripts\Lobby\Lobby.cs:101
0x00007FF6946BEEAC (Unity)
0x00007FF6946C2311 (Unity)
0x00007FF692E07F85 (Unity)
0x00007FF6953230BE (Unity)
0x00007FF694726B35 (Unity)

looking forward your reply

@hackiey Have a look at this forum thread:

Issue of unavailable thread context for main-thread-only unity functionalities when called from Namama Socket.ReceiveLoop() async events

In short you should use UnityMainThreadDispatcher or another library to “move” the execution of your scene change off the socket thread and back onto the Unity main thread:

https://github.com/PimDeWitte/UnityMainThreadDispatcher

1 Like

I handled this problem in a different way. I have a MatchEventHandler script that listens for match events. When socket events are fired the MatchEventHandler sets a boolean flag for the event to true. And within its own Update() method it checks for the flags and actions any events. This ensures they are always actioned on the unity main thread.