SceneManager LoadScene not working in editor playmode

Viewed 824

as the title says I have an issue loading a scene when in playmode in the editor.

The workflow of my game is as follows:

  1. Initializing (works fine) There is an empty scene that creates some global game objects that will exist during the entire runtime.
  2. MainMenu (works fine) After the Initializing is done it loads the MainMenu scene. I can interact with the scene and everything is nice.
  3. Connect to game server (works fine) In the main menu I have an option to connect to a game server (a dedicated server application I created on my own) Establishing the connection and sending the login works well.
  4. Character selection (not working) After the login on the server I get the resonse to select a character. (This works as expected.) Then I'm going to handle this response by opening the character selection scene. And here I have the issue. In Playmode inside the editor the handler method is executed (verifyed by debug logs) but the scene is not loaded actualy. When I build the game and run the created .exe and follow the exact same steps the character selection scene is loaded and shown as expected.

I searched the documentation and also the web but did not find any similar issues (maybe I still missed something)

So my question is as follows: How do I get the scene to also load in the editor playmode? My approach seems not to be totaly wrong as it works after build.

Here is the code snippet that should load the scene:

private void MessageRecived(object sender, GNL.ResponseMessageEventArgs e)
    {
        GameEventMessage message = this.eventManager.MessageHandler.ParseMessage(e.Message);

        Debug.Log($"Recived message with type {message.Type}");

        switch (message.Type)
        {
            case GameEvent.CharacterSelectionRequired:
                Debug.Log($"Handle character creation 01");
                this.HandleCharacterSelectionRequried(message);
                break;
            default:
                break;
        }
    }

    private void HandleCharacterSelectionRequried(GameEventMessage eventMessage)
    {
        Debug.Log($"Handle character creation 02");
        SceneManager.LoadScene("CharacterCreation");
    }

All three Debug.Log statements are executed. Only the LoadScene isn't working in the editor playmode.

IMPORTANT ADDITION After further testing I have to mention that the network communication is done in a seperate thread. From this thread when a new message arrives an eventHandler is called.

This is where the Method is added to the Event handler:

this.client = new GNL.GameClient(System.Net.IPAddress.Parse(host), port);

        this.client.AnnounceRecivedMessage += this.MessageRecived;

        this.clientNetwork = new Thread(this.client.Start);
        clientNetwork.Start();

And this is the definition of the eventHandler:

public event EventHandler<ResponseMessageEventArgs> AnnounceRecivedMessage;

IMPORTANT ADDITION - Part 2 I just discovered that, it works on a normal build but not when selecting "development build" in the build settings.

This is really annoying as I have to build the game every time I made a change to test it.

I'm thankful for any help and suggestions.

2 Answers

After much more debugging and testing I figured out that this issue indeed is a threading issue. Deep in the debugger I found the exception I would have expected for a threading issue. So I will have to change my implementation here.

But still there is an inconsistence in how threads are handled in Unity when running the game with debugging tools enabled (playmode in editor and development build) and with them disabled (normal build).

AFAIK, you can't switch between scenes in the editor. Unity Editor only works to edit and play the opened scene.

If you want to test some parts of the workflow, create functions dedicated to the editor (you can use #if UNITY_EDITOR) to test your scenes without the previous character selection.

Related