Search Unity

Third Party PUN Scene Sync not working

Discussion in 'Multiplayer' started by grayfalcon25, Apr 21, 2020.

  1. grayfalcon25

    grayfalcon25

    Joined:
    Apr 2, 2019
    Posts:
    3
    I'm trying to make a multiplayer car game. When you click on the Play button, you go to a sort of lobby, while you wait for another player. Once both the players have joined, in theory the players go to the game scene to race. But what actually happens is the first player stays in the lobby scene, and the second one goes to game scene. But there are two cars in the game scene, which leads me to believe it's a scene syncing issue. I was originally following along with the PUN Basics Tutorial, which is why some of the Debug messages don't make a lot of sense.

    Sorry, lot of code :/

    Launcher script: handles most of the networking
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Photon.Pun;
    5. using Photon.Realtime;
    6. using UnityEngine.UI;
    7.  
    8. namespace emmettitoi
    9. {
    10.   public class Launcher : MonoBehaviourPunCallbacks
    11.   {
    12.  
    13.       [SerializeField]
    14.       private GameObject controlPanel;
    15.  
    16.       public GameObject progressLabel;
    17.  
    18.       private float gameSize;
    19.  
    20.       public Text nameField;
    21.       public string rName;
    22.  
    23.       [SerializeField]
    24.       private byte maxPlayersPerRoom = 2;
    25.  
    26.       private bool isConnecting;
    27.  
    28.       void Awake()
    29.       {
    30.         PhotonNetwork.AutomaticallySyncScene = true;
    31.       }
    32.  
    33.       // Update is called once per frame
    34.       void Start()
    35.       {
    36.         controlPanel.SetActive(true);
    37.         progressLabel.SetActive(false);
    38.       }
    39.  
    40.       public void Connect()
    41.       {
    42.         gameSize = GameObject.Find("settings").GetComponent<settings>().gameSize;
    43.         rName = nameField.text;
    44.         Debug.Log("Room name: " + rName);
    45.         controlPanel.SetActive(false);
    46.         progressLabel.SetActive(false);
    47.         if (PhotonNetwork.IsConnected)
    48.         {
    49.           //PhotonNetwork.JoinRoom(rName);
    50.           PhotonNetwork.JoinRandomRoom();
    51.         }
    52.         else
    53.         {
    54.           isConnecting = PhotonNetwork.ConnectUsingSettings();
    55.         }
    56.       }
    57.       public override void OnConnectedToMaster()
    58.       {
    59.           Debug.Log("PUN Basics Tutorial/Launcher: OnConnectedToMaster() was called by PUN");
    60.           if (isConnecting)
    61.           {
    62.             //PhotonNetwork.JoinRoom(rName);
    63.             PhotonNetwork.JoinRandomRoom();
    64.             isConnecting = false;
    65.           }
    66.  
    67.       }
    68.       public override void OnDisconnected(DisconnectCause cause)
    69.       {
    70.         controlPanel.SetActive(true);
    71.         isConnecting = false;
    72.         Debug.LogWarningFormat("PUN Basics Tutorial/Launcher: OnDisconnected() was called by PUN with reason {0}", cause);
    73.       }
    74.       /*
    75.       public override void OnJoinRoomFailed(short returnCode, string message)
    76.       {
    77.           Debug.Log("PUN Basics Tutorial/Launcher:OnJoinRoomFailed() was called by PUN. No random room available, so we create one.\nCalling: PhotonNetwork.CreateRoom");
    78.  
    79.           // #Critical: we failed to join a random room, maybe none exists or they are all full. No worries, we create a new room.
    80.           PhotonNetwork.CreateRoom(rName, new RoomOptions { MaxPlayers = 2 });
    81.       }
    82.       */
    83.       public override void OnJoinRandomFailed(short returnCode, string message)
    84.       {
    85.           Debug.Log("PUN Basics Tutorial/Launcher:OnJoinRoomFailed() was called by PUN. No random room available, so we create one.\nCalling: PhotonNetwork.CreateRoom");
    86.  
    87.           // #Critical: we failed to join a random room, maybe none exists or they are all full. No worries, we create a new room.
    88.           PhotonNetwork.CreateRoom(rName, new RoomOptions { MaxPlayers = 2 });
    89.       }
    90.       public override void OnJoinedRoom()
    91.       {
    92.           Debug.Log("PUN Basics Tutorial/Launcher: OnJoinedRoom() called by PUN. Now this client is in a room.");
    93.           if (PhotonNetwork.CurrentRoom.PlayerCount == 1)
    94.           {
    95.               Debug.Log("we load lobby");
    96.  
    97.  
    98.               // #Critical
    99.               // Load the Room Level.
    100.               PhotonNetwork.LoadLevel("Lobby");
    101.           }
    102.       }
    103.  
    104.  
    105.   }
    106. }
    107.  
     
    vinayakmarv likes this.
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,068
    Sorry for not checking the code for you but:
    There are 2 demos in the PUN package, which might help: "Asteroids" and "Basics Tutorial" both switch between scenes.
     
  3. grayfalcon25

    grayfalcon25

    Joined:
    Apr 2, 2019
    Posts:
    3
    I was originally following the PUN Basics tutorial, then I attempted to convert it to my own game...
     
  4. vinayakmarv

    vinayakmarv

    Joined:
    Feb 16, 2022
    Posts:
    2
    This line might be the problem