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);
}
}
```