Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

PUN2 -- Loading Scene Issue, Only Loads One Scene

Discussion in 'Scripting' started by unity_8g7XwYU6kpaB-Q, Aug 24, 2019.

  1. unity_8g7XwYU6kpaB-Q

    unity_8g7XwYU6kpaB-Q

    Joined:
    Aug 17, 2019
    Posts:
    9
    I'm trying to make it so that on connection the new scene loads and the player is spawned. The switch is only there because the whole idea right now is beyond me. I've re-hauled everything after learning UNET is dead and I'm just trying to get this working after switching.

    I threw in debugs to see where my problem is. Here's the flow:
    Awake() is called and so is OnEnabled(). Then it tells me what scene is loaded, 0 is the buildIndex by the way, and that's normal, that's the menu scene. Then the connection to the master server begins and the Start() is called. then the player tries to join a room and creates one in the absence of one. The player then joins the room, StartGame() is called and so is OnDisabled(), finally at the end we find out we are still in scene 0. Before you ask, I have changed the LoadLevel from 0 to 1 and "Multiplayer" and even utilized multiplayerScene.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Photon.Pun;
    5. using Photon.Realtime;
    6. using System.IO;
    7. using UnityEngine.SceneManagement;
    8.  
    9. public class PhotonRoom : MonoBehaviourPunCallbacks, IInRoomCallbacks
    10. {
    11.     //Room info
    12.     public static PhotonRoom room;
    13.     private PhotonView PV;
    14.     public bool isGameLoaded;
    15.     public int currentScene;
    16.     public int multiplayerScene;
    17.     //Player info
    18.     Player[] photonPlayers;
    19.     public int playersInRoom;
    20.     public int myNumberInRoom;
    21.    
    22.     public int playersInGame;
    23.    
    24.     // Start is called before the first frame update
    25.    
    26.     void Awake()
    27.     {
    28.         Debug.Log("Awake() was called.");
    29.         if (PhotonRoom.room == null)
    30.         {
    31.             PhotonRoom.room = this;
    32.         }
    33.         else
    34.         {
    35.             if (PhotonRoom.room != this)
    36.             {
    37.                 Destroy(PhotonRoom.room.gameObject);
    38.                 PhotonRoom.room = this;
    39.             }
    40.             DontDestroyOnLoad(this.gameObject);
    41.         }
    42.            
    43.     }
    44.     public override void OnEnable()
    45.     {
    46.         base.OnEnable();
    47.         Debug.Log("OnEnable() was called.");
    48.         PhotonNetwork.AddCallbackTarget(this);
    49.         SceneManager.sceneLoaded += OnSceneFinishedLoading;
    50.  
    51.     }
    52.    
    53.     public override void OnDisable()
    54.     {
    55.         base.OnDisable();
    56.         Debug.Log("OnDisable() was called.");
    57.         PhotonNetwork.RemoveCallbackTarget(this);
    58.         SceneManager.sceneLoaded -= OnSceneFinishedLoading;
    59.         currentScene = SceneManager.GetActiveScene().buildIndex;
    60.         Debug.Log(currentScene);
    61.     }
    62.     void Start()
    63.     {
    64.         Debug.Log("PhotonRoom.cs was started.");
    65.         PV= GetComponent<PhotonView>();
    66.     }
    67.    
    68.     void OnSceneFinishedLoading(Scene scene, LoadSceneMode mode)
    69.     {
    70.         currentScene = scene.buildIndex;
    71.         Debug.Log(currentScene);
    72.         switch (currentScene)
    73.         {
    74.             case 1:
    75.                 Debug.Log("The Current Scene is the Multiplayer Scene");
    76.                 CreatePlayer();
    77.                 break;
    78.             case 0:
    79.                 Debug.Log("The Current Scene is the Menu Scene");
    80.                 break;
    81.         }
    82.    
    83.     }
    84.    
    85.     // Update is called once per frame
    86.     void Update()
    87.     {
    88.        
    89.     }
    90.    
    91.     public override void OnJoinedRoom()
    92.     {
    93.         base.OnJoinedRoom();
    94.         Debug.Log("We are now in a room.");
    95.         photonPlayers = PhotonNetwork.PlayerList;
    96.         playersInRoom = photonPlayers.Length;
    97.         myNumberInRoom = playersInRoom;
    98.         PhotonNetwork.NickName = myNumberInRoom.ToString();
    99.         StartGame();
    100.     }
    101.    
    102.     private void CreatePlayer()
    103.     {
    104.         Debug.Log("CreatePlayer() was called.");
    105.         PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs", "PhotonNetworkPlayer"), transform.position, Quaternion.identity, 0);
    106.     }
    107.    
    108.     void StartGame()
    109.     {
    110.         Debug.Log("StartGame() was called.");
    111.         isGameLoaded = true;
    112.         if (!PhotonNetwork.IsMasterClient)
    113.         {
    114.             return;
    115.         }
    116.         PhotonNetwork.LoadLevel("Multiplayer");
    117.     }
    118. }
    Take your time, I am in no rush. Thanks for your time.