Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Remove players and close server in Unity Multiplayer Game

Discussion in 'Multiplayer' started by siddharth3322, Jul 14, 2018.

  1. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,049
    I was working on two players small multiplayer game using Unity's multiplayer environment. I have completed whole game as I learnt through multiple places. Though still I have confusion regarding player removal from the game and releasing used multiplayer server from game.

    Let me give some details about my game play setup, its small multiplayer game so I have just keep only one game scene within it and multiplayer game play prefab, I was spawning to join multiplayer. Through NetworkManager my two players automatically get spawned within game play.

    networkmanager_inspector.png

    Now when any player get defeated, I have directly removed those through destroy method rather than using any multiplayer related method and Game Over screen become visible, still I have not destroyed NetworkManager instance.

    Code (CSharp):
    1. [Command]
    2. private void CmdShowGameOverPanel ()
    3. {
    4.     if (GameManager.Instance.GameType.Equals (GameConstants.GAME_TYPE_FRIENDS)) {
    5.         Camera.main.SendMessage (GameConstants.ACTIVATE_GAME_OVER_PANEL, true, SendMessageOptions.DontRequireReceiver);
    6.     }
    7.  
    8.     // server will receive command from other device client and call the RPC so other clients also become aware about same
    9.     if (isServer)
    10.         RpcShowGameOverPanel ();
    11. }
    12.  
    13. // if its server then it will call RPC
    14. // display game over screen
    15. [ClientRpc]
    16. private void RpcShowGameOverPanel ()
    17. {
    18.     if (GameManager.Instance.GameType.Equals (GameConstants.GAME_TYPE_FRIENDS)) {
    19.         Camera.main.SendMessage (GameConstants.ACTIVATE_GAME_OVER_PANEL, true, SendMessageOptions.DontRequireReceiver);
    20.     }
    21.  
    22.     // after game over screen displayed, destroy this ball player from all devices
    23.     Destroy (gameObject);
    24. }
    On my Game Over screen enable, I was calling StopClient on both connected devices and on Retry or Main Menu button press, I was calling StopHost method on both connected devices. After sometime of wait, I was destroying Multi player game prefab with NetworkManager in it and again respawning game play prefab to playing multiplayer game again.

    Code (CSharp):
    1. void OnEnable ()
    2. {
    3.     // only do in friends mode
    4.     if (GameManager.Instance.GameType.Equals (GameConstants.GAME_TYPE_FRIENDS))
    5.         DodgelsNetworkManager.singleton.StopClient ();
    6. }
    7.  
    8. public void OnRetryButtonClick ()
    9. {
    10.     SoundManager.Instance.PlayButtonClickSound ();
    11.  
    12.     // only do in friends mode
    13.     if (GameManager.Instance.GameType.Equals (GameConstants.GAME_TYPE_FRIENDS)) {
    14.    //           DodgelsNetworkManager.singleton.StopMatchMaker ();
    15.         DodgelsNetworkManager.singleton.StopHost ();
    16.  
    17.   //            DodgelsNetworkManager.singleton.StopServer ();
    18.     }
    19.  
    20.     StartCoroutine (RemoveLoadedGamePlay ());
    21.  
    22.     StartCoroutine (LoadGamePlay ());
    23. }
    But I am new person for Unity multiplayer setup so not sure whether I have done correct implementation or not!!! Because some time match making process get stuck and I require to close instance and try to join players again.

    So please give me some suggestion regarding removing players and closing server instance from game and start again when new prefab get spawned.