PhotonNetwork.Disconnect() Problem
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).
PhotonNetwork.Disconnect() Problem
Jesper21
2013-01-05 03:55:42
Hi. I have a problem. I wanna load a level after getting diconnected from a room. But i can't do this
PhotonNetwork.Disconnect();
Application.Loadlevel();
Because if i do that, i doesn't disconnect fast enough and loads the level without getting disconnected. I also can't use this
void OnPhotonPlayerDisconnected()
{
Application.Loadlevel();
}
Since that message only gets send to people inside the room that isn't disconnected. So i'm lost and can't find a logic to this one. Could use a timer, but if people lag or there is a server lag or something, that isn't gonna work. How could i make this?
Comments
There are two ways, depending on if you want to visualize it and how:
Use OnDisconnectedFromPhoton instead of OnPhotonPlayerDisconnected (see the Enums.cs file in the photon unity networking/plugins folder)
What you need to do is wait for connected to no longer be true and once thats the case you load the new level. Below is a quick hacked together code, not sure if connected really is present like that as my PUN version ain't the stock one anymore but it should be simple enough to understand :)
public void SwitchLevel (string level) { StartCoroutine (DoSwitchLevel(level)); }
IEnumerator DoSwitchLevel (string level) { PhotonNetwork.Disconnect (); while (PhotonNetwork.connected) yield return null; Application.LoadLevel(level); }
Thank you! Worked out perfect. Have a lovely day :)
Oh that is why, ty
4 years later and luckily it still works flawless
public void OnDisconnectedFromPhoton()
{
Debug.Log("OnPhotonPlayerDisconnected");
}
thanks
If anyone is reading this in 2019, I have used this great answer to create a similar solution after the latest PUN2 upgrade v2.16
You can check the end of this post.
@dreamora wrote: »
There are two ways, depending on if you want to visualize it and how:
Use OnDisconnectedFromPhoton instead of OnPhotonPlayerDisconnected (see the Enums.cs file in the photon unity networking/plugins folder)
What you need to do is wait for connected to no longer be true and once thats the case you load the new level.
Below is a quick hacked together code, not sure if connected really is present like that as my PUN version ain't the stock one anymore but it should be simple enough to understand :)public void SwitchLevel (string level)
{
StartCoroutine (DoSwitchLevel(level));
}IEnumerator DoSwitchLevel (string level)
{
PhotonNetwork.Disconnect ();
while (PhotonNetwork.connected)
yield return null;
Application.LoadLevel(level);
}
I've been struggling with this issue for the past 5 hours without any success, thank you so much, you are a Legend!
Blenderro247
2020-07-15 23:26:51
seven years later from this post and this helped with a problem i had today, disconnecting and reconnecting when the application goes into background, that coroutine rocks!
Techno_Babel
2020-08-22 17:59:32
Am I missing something? When I do this I get stuck in the loop forever...
PhotonNetwork.Disconnect();
while (PhotonNetwork.IsConnected)
{
Debug.Log("In loop");
yield return null;
}
SceneManager.LoadScene(0);
I can't break out of the loop.
Shredsauce
2020-09-02 01:12:07
@Techno_Babel wrote: »
Am I missing something? When I do this I get stuck in the loop forever...
PhotonNetwork.Disconnect(); while (PhotonNetwork.IsConnected) { Debug.Log("In loop"); yield return null; } SceneManager.LoadScene(0);
I can't break out of the loop.
Same here
Back to top