Search Unity

Question How to detect player disconnect or leave the game?

Discussion in 'Multiplayer' started by siddharth3322, Mar 3, 2022.

  1. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,049
    This is 2 players multiplayer game kind of like tic tac toe and multiplayer network I have used Photon.

    As per my question is mentioning, I want to detect the callback in two situations when the player is leaving the game by choice and player gets disconnected through break down of connection.

    For internet connection lose I don't have any event but I want to inform other player that opponent player is not in the game now.

    For player who want to leave the game by choice, for this I have placed main menu button within the gameplay screen.

    What to write in the main menu click event?

    My current scene structure of the game:
    tic tac toe game scenes.png
     
  2. maross334

    maross334

    Joined:
    Jul 28, 2021
    Posts:
    9
    You could use :
    Code (CSharp):
    1. OnPlayerLeftRoom (Player otherPlayer)
    This code will execute every time a player has left the room, and the parameter 'otherPlayer' is a reference to the player who left.

    If you have a netmanager or something that's calling the OnConnectedToMaster/OnJoinRoom/OnCreateRoom functions, I would put it there. You could raise an event or RPC to let other clients know about the player leaving, and just load a scene on the client who clicked the leave button. Hope that made sense!

    Edit:
    Assuming it is already, but for redundancy, make sure the class calling this inherits from MonoBehaviourPunCallbacks!