Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Why OnApplicationPause(true) is called when I reopen the app on Android?

Discussion in 'Scripting' started by iagoccampos, Sep 7, 2016.

  1. iagoccampos

    iagoccampos

    Joined:
    Aug 2, 2014
    Posts:
    10
    I'm developing a local multiplayer game and I'm having some troubles when the player press the middle button during the game. If you are the host when you press the "Home" button on android the client don't receive information and Unity don't accuses lost connection with the server.

    What I'm trying to do is use OnApplicationPause to send Rpc or Cmd saying that the player left the game, but OnApplicationPause(true) is called when the player reopen the app, I need it to be called just before the player press the middle button.

    Here is the code:

    Code (CSharp):
    1.  void OnApplicationPause(bool pauseStatus)
    2.      {
    3.          if(isLocalPlayer && pauseStatus)
    4.          {
    5.              GameController.otherPlayerGO.SetActive(false);
    6.              UIManager.Instance.ShowDialogPanel("You left the game!");
    7.              if(GameController.isServer)
    8.                  RpcPlayerQuit();
    9.              else
    10.                  CmdPlayerQuit();
    11.          }
    12.      }
    13.      [ClientRpc]
    14.      void RpcPlayerQuit()
    15.      {
    16.          if(!GameController.isServer)
    17.          {
    18.              GameController.otherPlayerGO.SetActive(false);
    19.              UIManager.Instance.ShowDialogPanel("The other player left!");
    20.          }
    21.      }
    22.      [Command]
    23.      void CmdPlayerQuit()
    24.      {
    25.          GameController.otherPlayerGO.SetActive(false);
    26.          UIManager.Instance.ShowDialogPanel("The other player left!");
    27.      }
    Well, any solution is welcome, that's all and sorry my bad english.