How to detect player disconnect or leave the game?

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

How to detect player disconnect or leave the game?

omar_altawil
2022-03-03 19:38:22

This is 2 players multiplayer game kind of like tic tac toe.

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:

Comments

Crazy416
2022-03-20 17:37:53

I think You Can add An Empty Object and a text in the searching players scene and add a script on it then try this code

public int PlayerCount;

int MaxPlayersPerRoom = 2;

[SerializeField] private Text alertText = null;

void Update()

{

PlayerCount = PhotonNetwork.CurrentRoom.PlayerCount;

if (PlayerCount != MaxPlayersPerRoom && WonPanelActive == false)

{

StartCoroutine(CheckingConnection());

}

}

IEnumerator CheckingConnection()

{

Debug.Log("A Player Disconnected");

yield return null;

}

and don't forget to add --> using System.Collections <-- at Top of the script and if it gives any error then try replacing

IEnumerator CheckingConnection()

{

Debug.Log("A Player Disconnected");

yield return new WaitForSeconds(0.2f);

    alertText.text = "Your Opponent Left";

}

Maybe This Will Work.

Back to top