MirrorNetworking / Mirror

#1 Open Source Unity Networking Library
https://mirror-networking.com
MIT License
5.15k stars 764 forks source link

networkSceneName is not changed when unloading a scene #3862

Closed Flawake closed 2 months ago

Flawake commented 2 months ago

I dont know if this is a bug or desired behaviour, but I beleive this is a bug. When additive loading scene B and then async unloading scene A the networkSceneName is still scene A which was unloaded.

  1. Change the scene for a player by loading the scene additive: (Code on server)

    conn.Send(new SceneMessage()
    {
    sceneName = "SceneB",
    sceneOperation = SceneOperation.LoadAdditive
    });
  2. Move the player object on the server: (Code on server) SceneManager.MoveGameObjectToScene(conn.identity.gameObject, SceneManager.GetSceneByName(data.requestedScene));

  3. Move the player on the client to the other scene and unload the original scene: (Code on client)

[Client]
public override void OnClientSceneChanged()
{
    base.OnClientSceneChanged();
    SceneManager.MoveGameObjectToScene(NetworkClient.connection.identity.gameObject, SceneManager.GetSceneByName("SceneB"));
    SceneManager.UnloadSceneAsync("SceneA");
}

networkSceneName is still sceneA altough it is unloaded

Expected behavior I expected networkSceneName to change to SceneB

Windows11 Unity version: 2022.3.21f1 Mirror version: 89.0.0

The code for setting the networkSceneName is in: https://github.com/MirrorNetworking/Mirror/blob/master/Assets/Mirror/Core/NetworkManager.cs line

As you can see it does change networkSceneName when not loading the scene additive

Or should I just set networkSceneName manually after unloading the old scene?

MrGadget1024 commented 2 months ago

Not a bug...you're not doing it correctly. networkSceneName is only changed when full scene changes are done through Mirror, e.g. NetworkManager.singleton.ServerChangeScene. Additive loading / unloading in your own code via Scene Manager doesn't change that.

Additionally, you don't do MoveGameObjectToScene on the client...only on server. On clients, players and spawned objects stay in the main container scene, not in the additive scenes.

For additional support, please join our Discord.