Continuing game once internet is disconnected and connected back again

I have implemented photon's Pun Solution for multiplayer in my Unity3d game and it is working fine except when I intentionally turn off my phone's data for few seconds and then turn it back on, the Player game object with PhotonView that I instantiated over the network destroys. Now I want a functionality where I can reconnect my player once internet connection is restored.
I am aware that using PhotonNetwork.ReconnectAndRejoin() connects the client to server back to server and then using PhotonNetwork.JoinRoom(LobbyController.RoomName) I can get connected back to the previous room. But the issue is how am I suppose to handle the player prefabs which were instantiated over the Photon network if I locally instantiate them then PhotonView has 0 as Id. Do I need to instantiate the Prefabs over the network again once internet connection is back? Please correct me if I misunderstood some functionality.
For checking internet I am using recursion, once internet is restored I reconnect and rejoin the room, I cached the room name.
public void CheckIfInternetIsBackOn()
    if (Application.internetReachability == NetworkReachability.NotReachable)
        Invoke("CheckIfInternetIsBackOn", 1);
        print("Reconnecting and rejoining");


  • Hi @MehtaNitish,

    Thank you for choosing Photon!

    PhotonNetwork.ReconnectAndRejoin(); no need for PhotonNetwork.JoinRoom(LobbyController.RoomName);

    This requires the room to have PlayerTTL different than 0.

    I would not use Application.internetReachability.
    I don't think it's reliable enough, see other solutions on the internet.
    Also, I would wait for the client to be disconnected first before trying to reconnect.
    There is a PUN callback for that.
  • Hi @JohnTube,

    Thanks for your answer!!

    So, I don't need to call PhotonNetwork.ReconnectAndRejoin();, and for PhotonNetwork.JoinRoom(LobbyController.RoomName); I will have it called in a coroutine which will have it checked if the player is already disconnected or not, is that what you mean?

    For PlayerTTL and EmptyRoomTTL, I have set it to 120000 during the first time room is created
    RoomOptions roomOptions = new RoomOptions() { IsVisible = true, IsOpen = true, MaxPlayers = (byte)maxPlayersInARoom };

    roomOptions.PlayerTtl = 120000; // 120 sec
    roomOptions.EmptyRoomTtl = 120000;

    My main question is that once player is connected back to the room, which I am able to do, how do I have networked player instantiated for my client, as on disconnect both the network players are destroyed(I have 2 player game with Max number of players in a room set to 2). If I use the local instantiate on the Player prefab with PhotonView, the PhotonView's ID is zero and when I instantiate player over the network I can only instantiate one player. How should I approach this problem of instantiating the players back on successful connection? Or is there a way so that players are not destroyed instantaneously when internet connection is out rather wait for a small amount of given time?
  • Hi @MehtaNitish,

    PUN's own instantiation is "buffered", it makes use of events caching to resend the event and redo the instantiation for rejoiners or late joiners.
    Are you using manual instantiation?

    You could try creating rooms with roomOptions.CleanupCacheOnLeave = false; if you do not want to destroy networked objects on leave.

  • Hi @JohnTube

    Thanks for your inputs, I added roomOptions.CleanupCacheOnLeave = false; where room is being created, but see no difference. My networked player is still being destroyed. Below is the whole room code:
    RoomOptions roomOptions = new RoomOptions() { IsVisible = true, IsOpen = true, MaxPlayers = (byte)maxPlayersInARoom };

    roomOptions.PlayerTtl = 120000; // 120 sec
    roomOptions.EmptyRoomTtl = 120000;
    roomOptions.CleanupCacheOnLeave = false;

    Is there anything else I can do here?

  • Hi @MehtaNitish,

    Are you using PUN Classic?
    if so, there is also PhotonNetwork.autoCleanUpPlayerObjects which should be set to false.
  • Hi @JohnTube

    No, not PUN Classic. I am using Photon PUN 2, free one present on Unity Asset store.
  • Hi @MehtaNitish,

    How do you instantiate networked objects in the first place?
    Do you use manual instantiation or you use PhotonNetwork.Instantiate?
  • Hi,

    it's also important to check the result of ReconnectAndRejoin:

    if false, then it's not going to work.

    I would try to Reconnect() first and then rejoin().


Sign In or Register to comment.