0
\$\begingroup\$

I've been trying to make a multiplayer game with mirror networking. The Network Manager works correctly when it comes to changing the scene, but the OnServerSceneChanged call just doesn't trigger when the scene changes. I've checked for Network Manager deletion, whether I used the correct function (OnServerChangeScene), but nothing works. The functions work correctly to transition to the Game scene, it's only OnServerSceneChanged that is not called.

    public void StartTransitionScene() //sets scene to loading scene
    {
        if (SceneManager.GetActiveScene().path == menuScene)
        {
            if (!IsReadyToStart()) { return; }

            ServerChangeScene("Game");
        }
    }


    public override void ServerChangeScene(string newSceneName)
    {
        if (SceneManager.GetActiveScene().path == menuScene && newSceneName.Contains("Game"))
        {
            for (int i = RoomPlayers.Count - 1; i >= 0; i--) //converts room players to game players
            {
                var conn = RoomPlayers[i].connectionToClient;
                var gameplayerInstance = Instantiate(gamePlayerPrefab);
                gameplayerInstance.SetDisplayName(RoomPlayers[i].DisplayName);

                NetworkServer.Destroy(conn.identity.gameObject);

                NetworkServer.ReplacePlayerForConnection(conn, gameplayerInstance.gameObject);
            }
        }

        base.ServerChangeScene(newSceneName);
    }

    public override void OnServerSceneChanged(string sceneName)
    {
        Debug.Log("SCENE CHANGED");
        if (sceneName.StartsWith("Game"))
        {
            GameObject playerSpawnSystemInstance = Instantiate(playerSpawnSystem);
            NetworkServer.Spawn(playerSpawnSystemInstance);
        }
    }

Edit:

Called the callback as a function, it displayed a debug, edited code:

    public override void ServerChangeScene(string newSceneName)
    {
        if (SceneManager.GetActiveScene().path == menuScene && newSceneName.Contains("Game"))
        {
            for (int i = RoomPlayers.Count - 1; i >= 0; i--) //converts room players to game players
            {
                var conn = RoomPlayers[i].connectionToClient;
                var gameplayerInstance = Instantiate(gamePlayerPrefab);
                gameplayerInstance.SetDisplayName(RoomPlayers[i].DisplayName);

                NetworkServer.Destroy(conn.identity.gameObject);

                NetworkServer.ReplacePlayerForConnection(conn, gameplayerInstance.gameObject);
            }
        }

        base.ServerChangeScene(newSceneName);
        OnServerSceneChanged("Game");
    }


    public override void OnServerSceneChanged(string sceneName)
    {
        Debug.Log("Executed");
        if(sceneName.Contains("Game"))
        {
            GameObject playerSpawnSystemInstance = Instantiate(playerSpawnSystem);
            NetworkServer.Spawn(playerSpawnSystemInstance);
        }
    }

```
\$\endgroup\$
10
  • \$\begingroup\$ This is code for the host or server? Because on client that callback does not exist \$\endgroup\$
    – Zibelas
    Commented Aug 15, 2023 at 15:42
  • \$\begingroup\$ Yes, this code is for the server, it's inherited from NetworkManager \$\endgroup\$
    – koogel
    Commented Aug 16, 2023 at 17:24
  • \$\begingroup\$ And are you calling it on the server as well or just from the client? To make a request from a client to the server, you need to use commands \$\endgroup\$
    – Zibelas
    Commented Aug 16, 2023 at 17:50
  • \$\begingroup\$ I am calling it from the server(Network Manager Lobby inherited from NetworkManager) they are in the same script. \$\endgroup\$
    – koogel
    Commented Aug 16, 2023 at 18:31
  • 1
    \$\begingroup\$ Thats not how network/ client-server code is supposed to work. Yes you can call it directly but than it still does not get executed on the server. Yes, you see the Debug, but you see it on the client. How it is supposed to be is that the host or client with the rights is telling/ asking the server: we are all ready, may we change the scene? And the server is: sure, let me do that for you. What you have is: I (the client) is gonna change the scene. (but nobody else knows about it since the client can't tell that to other clients) \$\endgroup\$
    – Zibelas
    Commented Aug 16, 2023 at 19:56

0

You must log in to answer this question.

Browse other questions tagged .