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! Your search result can be found below. Plus, we offer support via 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.

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

Because if i do that, i doesn't disconnect fast enough and loads the level without getting disconnected. I also can't use this

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

dreamora
2013-01-06 09:45:05

There are two ways, depending on if you want to visualize it and how:

  1. Use OnDisconnectedFromPhoton instead of OnPhotonPlayerDisconnected (see the Enums.cs file in the photon unity networking/plugins folder)

  2. 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); }

Jesper21
2013-01-06 18:51:36

Thank you! Worked out perfect. Have a lovely day :)

Dannark
2017-03-12 23:43:06

Oh that is why, ty

kk99
2017-11-06 21:37:02

4 years later and luckily it still works flawless

thanks

BFX
2020-02-02 16:04:10

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.

Migmac
2020-06-16 01:45:30

@dreamora wrote: »

There are two ways, depending on if you want to visualize it and how:

  1. Use OnDisconnectedFromPhoton instead of OnPhotonPlayerDisconnected (see the Enums.cs file in the photon unity networking/plugins folder)

  2. 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...

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...

I can't break out of the loop.

Same here

Back to top