How to disconnect from PhotonNetwork Completely and Correctly

Options

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


So how do I exactly disconnect completely before loading the new scene?

Answers

  • Klover
    Options

    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?