Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Third Party Mirror networking - OnServerSceneChanged not triggering when scene changes

Discussion in 'Multiplayer' started by koogelpotato, Aug 14, 2023.

  1. koogelpotato

    koogelpotato

    Joined:
    Feb 20, 2020
    Posts:
    6
    Hello, I've been experiencing an issue with Mirror network manager. I override the function of OnServerSceneChanged, but it doesn't trigger when I change my scene from lobby to game. OnServerSceneChanged:

    Code (CSharp):
    1. public override void OnServerSceneChanged(string sceneName)
    2.     {
    3.         if(sceneName.Contains("Game"))
    4.         {
    5.             GameObject playerSpawnSystemInstance = Instantiate(playerSpawnSystem);
    6.             NetworkServer.Spawn(playerSpawnSystemInstance);
    7.         }
    8.     }
    The server scene changes correctly, Network Manager persists in both scenes and does not get destroyed.
    Code that executes correctly:
    Code (CSharp):
    1. public void StartTransitionScene() //sets scene to loading scene
    2.     {
    3.         if (SceneManager.GetActiveScene().path == menuScene)
    4.         {
    5.             if (!IsReadyToStart()) { return; }
    6.  
    7.             ServerChangeScene("Game");
    8.         }
    9.     }
    10.  
    11.  
    12.     public override void ServerChangeScene(string newSceneName)
    13.     {
    14.         if (SceneManager.GetActiveScene().path == menuScene && newSceneName.Contains("Game"))
    15.         {
    16.             for (int i = RoomPlayers.Count - 1; i >= 0; i--) //converts room players to game players
    17.             {
    18.                 var conn = RoomPlayers[i].connectionToClient;
    19.                 var gameplayerInstance = Instantiate(gamePlayerPrefab);
    20.                 gameplayerInstance.SetDisplayName(RoomPlayers[i].DisplayName);
    21.  
    22.                 NetworkServer.Destroy(conn.identity.gameObject);
    23.  
    24.                 NetworkServer.ReplacePlayerForConnection(conn, gameplayerInstance.gameObject);
    25.             }
    26.         }
    27.  
    28.         base.ServerChangeScene(newSceneName);
    29.     }
    30.  
    31.     public override void OnServerReady(NetworkConnectionToClient conn)
    32.     {
    33.         base.OnServerReady(conn);
    34.  
    35.         OnServerReadied?.Invoke(conn);
    36.     }
    Becase of it the players don't spawn in the game scene. I tested wether the player spawn point script was an issue, but it works correctly, as the OnServerReady function invokes a method there.