Home > Net >  Unity - Mirror Networking : Player client disconnect after scene changed
Unity - Mirror Networking : Player client disconnect after scene changed

Time:10-22

I'm currently using Mirror Networking to make a multiplayer game. I have a scene when players are all connected, they can choose their characters and set the ready. If all players are ready, I change current scene to arena scene using MyNetworkManager.ServerChangeScene(arenaSceneName). This method sets all player clients as not ready. But After the scene was loaded, my player client is no longer connected to my host and I don't know why.

Can you help me please ?

Thanks a lot for answers.

CodePudding user response:

public class ReadyPlayerChecker : NetworkBehaviour
{
  public List<PlayerBehaviour> activePlayers;

  public List<PlayerBehaviour> GetActivePlayers()
  {
    return activePlayers;
  }

  // Start is called before the first frame update
  void Start()
  {
    activePlayers = new List<PlayerBehaviour>();
  }

  // Update is called once per frame
  void Update()
  {
    foreach (PlayerBehaviour player in FindObjectsOfType<PlayerBehaviour> 
    (true))
    {
       if(!activePlayers.Contains(player))
       {
          activePlayers.Add(player);
       }
    }

    bool allPlayersReady = true;
    foreach (PlayerBehaviour player in activePlayers)
    {
       if (!player.IsReady())
       {
          allPlayersReady = false;
       }
    }
    if (allPlayersReady && activePlayers.Count > 0)
    {
       OnAllPLayersReady();
    }
 }

 [Command]
 public void OnAllPLayersReady()
 {
    GameObject.Find("SceneManager").GetComponent<SceneChanger> 
    ().LoadScene("SimpleArena");
 }

}

CodePudding user response:

Clients that connect to this server will automatically switch to this scene. This is called automatically if onlineScene or offlineScene are set, but it can be called from user code to switch scenes again while the game is in progress. This automatically sets clients to be not-ready. The clients must call NetworkClient.Ready again to participate in the new scene."

if its not solve problem can you give more information about your project Are you using MatchInterestManager ? This can be lead some problems like yours

Edit 1 - Maybe the problem was host can you run your code on server not host

Edit 2 - i belive your command function runs on client not server because you miss "Cmd" prefix on your command function change it CmdOnAllPlayersReady()

CodePudding user response:

This is my Network Manager settings :

My Network Manager settings

  • Related