Search Unity

Third Party PUN2 second start game problem

Discussion in 'Multiplayer' started by MaciejKusztal, Jun 12, 2021.

?

How to start match without problems and freeze the scene?

  1. Solve this problem.

    0 vote(s)
    0.0%
  2. Solve this problem.

    0 vote(s)
    0.0%
Multiple votes are allowed.
  1. MaciejKusztal

    MaciejKusztal

    Joined:
    Feb 4, 2020
    Posts:
    87
    Hi,
    When I scene start to connect with the Master server after than when I press the button it joins to room or creates new, but find the opponent! Then change the scene and start the match.
    When the player leaves the game, I can find the opponent again but when starting the match in the next scene everything is freeze (like pause).
    I'm using android device.
    Why??

    Code (CSharp):
    1. using Photon.Pun;
    2. using Photon.Realtime;
    3. using TMPro;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7. public class FindOpponentManager : MonoBehaviourPunCallbacks
    8. {
    9.     #region Variables
    10.  
    11.     public TMP_Text waitingStatusText = null;
    12.     public Animator _animator;
    13.  
    14.     private bool isConnecting = false;
    15.     private bool wasConnected;
    16.     private const string GameVersion = "1.0.1";
    17.     private const int maxPlayersPerRoom = 2;
    18.  
    19.     public GameObject _storeIcons;
    20.     private int _countdown = 3;
    21.  
    22.     public EnergyManager energyManager;
    23.     public Button _GAbutton;
    24.     public TMP_Text _ownerNickname;
    25.     public TMP_Text _opponentNickname;
    26.     public Button _leaveRoomButton;
    27.     int gameUser = 0;
    28.  
    29.     #endregion
    30.  
    31.     private void Awake() => PhotonNetwork.AutomaticallySyncScene = true;
    32.  
    33.     private void Start()
    34.     {
    35.         if(PhotonNetwork.IsConnected)
    36.             wasConnected = true;
    37.         else
    38.             wasConnected = false;
    39.  
    40.         if(wasConnected == true)
    41.         {
    42.             PhotonNetwork.ReconnectAndRejoin();
    43.         }
    44.  
    45.     }
    46.  
    47.     private void Update()
    48.     {
    49.  
    50.         if (PhotonNetwork.IsConnected)
    51.         {
    52.             _GAbutton.enabled = true;
    53.         }
    54.         else
    55.         {
    56.             _GAbutton.enabled = false;
    57.             PhotonNetwork.GameVersion = GameVersion;
    58.             PhotonNetwork.ConnectUsingSettings();
    59.         }
    60.     }
    61.  
    62.     public void OnClick_FindOpponent()
    63.     {
    64.         isConnecting = true;
    65.         if (energyManager.totalEnergy > 0)
    66.         {
    67.             energyManager.UseEnergy();
    68.             energyManager.UpdateEnergy();
    69.             energyManager.UpdateTimer();
    70.  
    71.             _animator.Play("findopponent_open");
    72.             _ownerNickname.text = PlayerPrefs.GetString("Username");
    73.  
    74.             waitingStatusText.text = "Searching ...";
    75.  
    76.             if (PhotonNetwork.IsConnected)
    77.             {
    78.                 PhotonNetwork.JoinRandomRoom();
    79.             }
    80.         }
    81.         else if(energyManager.totalEnergy == 0)
    82.         {
    83.             isConnecting = false;
    84.             _animator.Play("store_open_noEnergy");
    85.             return;
    86.         }
    87.     }
    88.  
    89.     public override void OnConnectedToMaster()
    90.     {
    91.         Debug.Log("Connected To Master");
    92.         isConnecting = true;
    93.     }
    94.  
    95.     public override void OnDisconnected(DisconnectCause cause)
    96.     {
    97.         Debug.Log($"Disconnect due to: {cause}");
    98.         isConnecting = false;
    99.     }
    100.  
    101.     public override void OnJoinRandomFailed(short returnCode, string message)
    102.     {
    103.         Debug.Log("No clients are waiting for an opponent, creating new room");
    104.  
    105.         PhotonNetwork.CreateRoom(null, new RoomOptions { MaxPlayers = maxPlayersPerRoom });
    106.     }
    107.  
    108.     public override void OnJoinedRoom()
    109.     {
    110.         Debug.Log("Client successfully joined a room");
    111.  
    112.         int playerCount = PhotonNetwork.CurrentRoom.PlayerCount;
    113.  
    114.         if (playerCount != maxPlayersPerRoom)
    115.         {
    116.             waitingStatusText.text = "Waiting For Opponent";
    117.             Debug.Log("Client is waiting for an opponent");
    118.         }
    119.         else
    120.         {
    121.             waitingStatusText.text = "Opponent Found";
    122.             _leaveRoomButton.gameObject.SetActive(false);
    123.             photonView.RPC("OpponentNickname", RpcTarget.Others, gameUser);
    124.             Debug.Log("Matching is ready to begin");
    125.         }
    126.     }
    127.  
    128.     public override void OnPlayerEnteredRoom(Player newPlayer)
    129.     {
    130.         if (PhotonNetwork.CurrentRoom.PlayerCount == maxPlayersPerRoom)
    131.         {
    132.             PhotonNetwork.CurrentRoom.IsOpen = false;
    133.             Debug.Log("Match is ready to begin");
    134.  
    135.             waitingStatusText.text = "Opponent Found";
    136.             Debug.Log("Match is ready begin");
    137.  
    138.             PhotonNetwork.LoadLevel("GlobalArena");
    139.         }
    140.     }
    141.  
    142.     public void OnClick_LeaveTheRoom()
    143.     {
    144.         PhotonNetwork.LeaveRoom();
    145.         _animator.Play("findopponent_close");
    146.         energyManager.totalEnergy += 1;
    147.         energyManager.UpdateEnergy();
    148.         energyManager.UpdateTimer();
    149.     }
    150.  
    151.     public override void OnLeftRoom()
    152.     {
    153.         Debug.Log("Player left the photon room");
    154.     }
    155.  
    156.     [PunRPC]
    157.     private void OpponentNickname(int gameUserParametr)
    158.     {
    159.         if (gameUserParametr == 0)
    160.         {
    161.             _opponentNickname.text = PlayerPrefs.GetString("Username");
    162.         }
    163.     }
    164.  
    165.     private void OnApplicationQuit()
    166.     {
    167.         PhotonNetwork.Disconnect();
    168.     }
    169. }
     
    Last edited: Jun 14, 2021