How to disconnect from PhotonNetwork Completely and Correctly
The whole answer can be found below.
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).
How to disconnect from PhotonNetwork Completely and Correctly
ChethanV007
2022-02-08 08:16:36
Right I found this question asked elsewhere but I am really stuck at the point where none of the solution is unfortunately working.
So when the game gets over, I have to load either the menu screen, random battle screen or the friends screen. But somehow I am trying my heart out to disconnect when I click the random screen button and friends screen button. But it is just not disconnecting.
The code is as follows:
public void OnClickRandomScreenButton()
{
MultiPlayerScoreManager.instance.NextSceneToBeLoaded = "MatchMakingScreen";
PhotonNetwork.LeaveRoom();
}
public void OnClickFriendsScreenButton()
{
MultiPlayerScoreManager.instance.NextSceneToBeLoaded = "FriendsScreen";
PhotonNetwork.LeaveRoom();
}
public override void OnLeftRoom()
{
Debug.Log("On left room executed in Game Over Manger class.........");
base.OnLeftRoom();
PhotonNetwork.Disconnect();
}
public override void OnDisconnected(DisconnectCause cause)
{
Debug.Log("On OnDisconnected executed in Game Over Manger class.........");
base.OnDisconnected(cause);
SceneManager.LoadScene( MultiPlayerScoreManager.instance.NextSceneToBeLoaded );
}
I basically what I am doing is first leave the room, and when I left the room, then disconnect and load the scene. I tried putting PhotonNetwork.Disconnect(); in the functions where the button click is listened but I read somewhere that it takes time and all of that.
I tried to disconnect from the PhotonNetwork in the matchmaking scene in start like:
if(PhotonNetwork.isConnected)
PhotonNetwork.Disconnect();
ConnectToPhoton(Username);
But it is just saying connecting to server and not proceeding.
In my game I have to disconnect before connecting again but somehow it is not happening.
I tried using this solution but it is getting into an infinite loop.
The error basically which I am getting is operation leaveRoom(254) not called because Client is not............ I am attaching the screen shot below
The whole part is when I stop playing in Unity, then the OnLeftRoom and OnDisconnected function gets executed . I have highlighed with a red rectangle around it in the screenshot
Uploaded 2022-02-08T08:16:02+00:00 171912 bytes
So how do I exactly disconnect completely before loading the new scene?
Comments
But why are you disconnecting from Photon Network completely during this event? Shouldn't you just simply leave the room and then load up your scene?
Anyways, I believe if you use PhotonNetwork.LoadLevel(); to load your scenes, if you are disconnected it will automatically send you back to the first scene.
ChethanV007
2022-02-09 11:55:25
Klover 2022-02-09T10:49:21+00:00
But why are you disconnecting from Photon Network completely during this event? Shouldn't you just simply leave the room and then load up your scene?
Anyways, I believe if you use PhotonNetwork.LoadLevel(); to load your scenes, if you are disconnected it will automatically send you back to the first scene.
Actually I am fine even if you just leave the room, but OnRoomLeft() callback doesnt get executed at all.I didnt understand your second part of the answer. Would u mind elaborating it?
Back to top