How to transfer ownership when lock screen?

Options
Hi, I am making my first game using PUN 2. There are two people in the room fighting each other. When one of the players hide the application or locks the screen, I need to transfer ownership of its GameObject to the second player. For this I use Unity's OnApplicationFocus function. The documentation states that after calling the RPC function, I need to use PhotonNetwork.SendAllOutgoingCommands (), but when the player is called, the photon is disconnected. As I think by the number of attempts to send a message. This usually happens when the screen is locked. How can I fix this?

Answers

  • Ravindra5337
    Options
    You have to make use of OnApplicationPause method of Unity Monobehaviour for this one. this will be called when user hides app or locks the screen. when game is paused just change the Master client and call SendAllOutgoingCommands to send this event immediately.
    public void OnApplicationPause(bool pause)
        {
            
            if (pause)
            {
                if (PhotonNetwork.IsMasterClient)
                {
                    PhotonNetwork.SetMasterClient(PhotonNetwork.LocalPlayer.GetNext());
                    PhotonNetwork.SendAllOutgoingCommands();
                }
    
            }
      }
    

    also make sure you have enabled Run in background in PhotonServerSettings scriptable object.