Search Unity

Scenes with netcode in single mode are not reloaded on late connected clients

Discussion in 'Multiplayer' started by Mauschelbaer, Jan 22, 2023.

  1. Mauschelbaer

    Mauschelbaer

    Joined:
    Aug 4, 2019
    Posts:
    17
    I'm trying to build up the basic functionality of a multiplayer game with netcode.
    However, I still have problems with netcode and understand it properly.

    I would like to go directly "in-game" as a host and if a client joins/connects later,
    should this also come directly in-game.

    Since Netcode takes over the scene management for me, this should be possible via
    NetworkManager.SceneManager.LoadScene
    . Here is a minimal example:

    Code (CSharp):
    1.  
    2.     public void StartGameAsHost(string profileId)
    3.     {
    4.         NetworkManager.Singleton.StartHost();
    5.  
    6.         if (IsServer)
    7.         {
    8.             NetworkManager.SceneManager.LoadScene("TestMap01", LoadSceneMode.Additive);
    9.         }
    10.  
    11.     }
    12.  
    13.     public void JoinGameAsClient(string profileId)
    14.     {
    15.         NetworkManager.Singleton.StartClient();
    16.     }
    17.  
    That works. But only as long as I selected LoadSceneMode.Additive. The client then builds exactly the same scenes. However, if I switch to LoadSceneMode.Single, nothing seems to change on the client after the connect, i.e. no scene is loaded.

    My second problem is that I would like to load multiple Additive Scenes, but unlike the local one, I can only load one at a time. Is it not possible to load multiple complementary scenes via network?

    Code (CSharp):
    1.  
    2.     NetworkManager.SceneManager.LoadScene("TestMap01", LoadSceneMode.Additive);
    3.     NetworkManager.SceneManager.LoadScene("Gameplay", LoadSceneMode.Additive);
    4.    
    What can be the reason? Maybe someone can help me?
     
  2. Mauschelbaer

    Mauschelbaer

    Joined:
    Aug 4, 2019
    Posts:
    17
    Okay, I found out that using single does work. Nothing was displayed visibly, because the complete replacement of a scene logically deletes all non-DDoL GameObjects on both the server and client side, which in my case meant that there was no camera in the new scene.:confused:

    However, my second problem with the multiple additive scenes remains
     
  3. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,892
    Generally speaking it is best to not do anything straight after calling StartHost/Server but rather delay anything (ie load scene) to the usual callback event such as client connected or OnNetworkSpawn.

    Similarly I can imagine (just a hunch) that you cannot issue multiple additive scene loads right after another. Instead wait for the „scene was loaded“ callback and then load another scene additively.
     
    Mauschelbaer likes this.