How to disconnect from the server and connect for the second game?
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 the server and connect for the second game?
sic
2021-06-29 17:19:55
Hi,
I use Photon (PUN2) in my game. I game all system and its works (when you play the first game). The user connects to a server, lobby etc. Game, after the game user, should disconnect from the server and when wants play again just connect again to the server. Problem because the user can't disconnect. Why?
First code, when Player finish the game.
I use this function when the user is in the Lobby and join or create the room. The user has possible to leave the room.
When he/she presses the button
"User left from Lobby"
"Disconnect from the server"
" ConnectUsingSettings() failed. Can only connect while in state 'Disconnected'. Current state: Connected".
https://paste.myst.rs/dy4wm66j
Comments
You only need to call PhotonNetwork.Disconnect and wait for the disconnected callback.
If you didn't update PUN 2 in a while, please do. This might help avoid issues we fixed before.
Related Unity forum thread.
Yes, but when I want to come back to the server and play again I can't.
Have a look at the callstack for the error "ConnectUsingSettings() failed". This is called "too early". When you do a regular disconnect from the server, the client will tell the server it is diconnecting and this takes some communication. Wait for the callback OnDisconnected, before you call ConnectAndJoinRandom anywhere.
I did a small break after disconnect (5 seconds) and before the change of the scene, but it doesn't help. When I would like to start the next game I can't connect with the server and go to the lobby.
Which version of PUN do you use? If it's not recent, please update.
Can you reproduce this with some of our demos? Without more concrete info, I have a hard time answering something useful...