Search Unity

Question Network SceneManager

Discussion in 'Netcode for GameObjects' started by TruckerJoe, Dec 9, 2022.

  1. TruckerJoe

    TruckerJoe

    Joined:
    Feb 25, 2019
    Posts:
    39
    Hi!
    I have big problems to change my scene from my lobby to the game scene.
    In my first scene (MainMenu) I have my Network Manager.
    From that I click on a Multiplayer Button and switch the scene with:
    Code (CSharp):
    1. SceneManager.LoadScene("Scene_Lobby");
    to my Lobby Scene.
    In my Lobby Scene I have a gameobject with a ProjectSceneManger Script attached to it with this code:
    Code (CSharp):
    1. public override void OnNetworkSpawn()
    2.     {
    3.         if (IsServer && !string.IsNullOrEmpty(m_SceneName))
    4.         {
    5.             Debug.Log("OnNetworkSpawn");
    6.  
    7.             NetworkManager.Singleton.SceneManager.OnSceneEvent += SceneManager_OnSceneEvent;
    8.             var status = NetworkManager.Singleton.SceneManager.LoadScene(m_SceneName, LoadSceneMode.Additive);
    9.             Debug.Log(status);
    10.  
    11.             CheckStatus(status);
    12.         }
    13.        
    14.         base.OnNetworkSpawn();
    15.     }
    I try to start my game from the the lobby with
    Code (CSharp):
    1. NetworkManager.Singleton.StartHost();
    for the host and
    Code (CSharp):
    1. NetworkManager.Singleton.StartClient();
    for the client,
    but the debug from the OnNetworkSpawn methode always prints:
    Code (CSharp):
    1. Failed to load Scene_Game with a SceneEventProgressStatus: SceneEventInProgress
    2.  
    The docs say:
    • This is returned if you attempt to start a new scene event (loading or unloading) while one is still in progress
    but I don´t understand why there should be a loading process.
    Can anyone help me?
     
  2. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    665
    It sounds like LoadScene is called too early but I'm sure why that is, you could try moving it to Start instead.

    If that doesn't work try subscribing to SceneManager.sceneLoaded and the NetworkSceneManager callbacks to shed more light on the subject.
    Code (CSharp):
    1.        SceneManager.sceneLoaded += (scene, mode) =>
    2.         {
    3.             Debug.Log($"SceneManager scene: {scene.name} mode: {mode}");
    4.         };
    Code (CSharp):
    1.         NetworkManager.Singleton.SceneManager.OnLoad += OnLoad;
    2.         NetworkManager.Singleton.SceneManager.OnLoadComplete += OnLoadComplete;
    3.         NetworkManager.Singleton.SceneManager.OnLoadEventCompleted += OnLoadEventCompleted;
    4.         NetworkManager.Singleton.SceneManager.OnSceneEvent += OnSceneEvent;
    5.         NetworkManager.Singleton.SceneManager.OnSynchronize += OnSynchronize;
    6.         NetworkManager.Singleton.SceneManager.OnSynchronizeComplete += OnSynchronizeComplete;
    7.         NetworkManager.Singleton.SceneManager.OnUnload += OnUnload;
    8.         NetworkManager.Singleton.SceneManager.OnUnloadComplete += OnUnloadComplete;
    9.         NetworkManager.Singleton.SceneManager.OnUnloadEventCompleted += OnUnloadEventCompleted;
     
  3. TruckerJoe

    TruckerJoe

    Joined:
    Feb 25, 2019
    Posts:
    39
    Thank you very much!
    I found out, that I have loaded also a scene in the ApprovalCheck:rolleyes:
    After I changed that, the host is changing the scene correctly, but the connected client not.
    Do I have to call the NetworkManager.Singleton.SceneManager.LoadScene method from the client also, or
    what could be wrong here?
     
  4. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    665
    The client scene change should be automatic if Enable Scene Management is ticked on the NetworkManager (it is by default) and the connection is approved. If you subscribe to NetworkManager.Singleton.OnClientConnectedCallback on client/server it will be called on client connection.
     
  5. TruckerJoe

    TruckerJoe

    Joined:
    Feb 25, 2019
    Posts:
    39
    Now it is working!
    I changed so much back and forth, that I can´t say what exactly the reason was.
    Thank you so much cerestorm!
     
    cerestorm likes this.