Double Prefab instantiation mystery

Options
Hello,

I've just finished the Photon (PUN) tutorial, and I'm having a couple of issues. One I'm trying to solve is when my little robot man has a second player join him, and join "Room for 2", it instantiates a new prefab at the origin point. The problem is the instance from "Room for 1" is still in the scene, and now player one is controlling 2 prefabs...
If a third player joins, players 1 and 2 now both get an extra prefab. So player 1 is up to 3 prefabs, and player 2 has 2. This has stacking results.

I wonder if anyone else has encountered this issue and if they had come up with a solution.

I'm not sure exactly what code to share for this, so I will share a couple methods from "Game Manager.cs" where I think the problem may lie...

        // Called when the local player left the room. We need to load the launcher scene.

        public override void OnPlayerEnteredRoom(Player other)
        {
            Debug.LogFormat("OnPlayerEnteredRoom() {0}", other.NickName); // not seen if you're the joining player

            if (PhotonNetwork.IsMasterClient)
            {
                // called before OnPlayerLeftRoom
                Debug.LogFormat("OnPlayerEnteredRoom IsMasterClient {0}", PhotonNetwork.IsMasterClient);

                LoadArena();
            }
        }

        public override void OnPlayerLeftRoom(Player other)
        {
            Debug.LogFormat("OnPlayerLeftRoom() {0}", other.NickName);

            if (PhotonNetwork.IsMasterClient)
            {
                Debug.LogFormat("OnPlayerLeftRoom IsMasterClient {0}", PhotonNetwork.IsMasterClient);

                LoadArena();
            }
        }


        void LoadArena()
        {
            if (!PhotonNetwork.IsMasterClient)
            {
                Debug.LogError("PhotonNetwork: Trying to Load a level but we are not the master client.");
            }

            Debug.LogFormat("PhotonNetwork: Loading Level : {0}", PhotonNetwork.CurrentRoom.PlayerCount);
            PhotonNetwork.LoadLevel("Room for " + PhotonNetwork.CurrentRoom.PlayerCount);
        }

Comments

  • ChimeraBlack
    edited April 2019
    Options
    I figured it out. I just destroy everything right before I load a level. Nothing will carry over. woot.
            void LoadArena()
            {
                if (!PhotonNetwork.IsMasterClient)
                {
                    Debug.LogError("PhotonNetwork: Trying to Load a level but we are not the master client.");
                }
    
                Debug.LogFormat("PhotonNetwork: Loading Level : {0}", PhotonNetwork.CurrentRoom.PlayerCount);
                PhotonNetwork.DestroyAll();   //<----------------- KILL EVERYTHING THE PREVIOUS ROOM EVER LOVED.
                PhotonNetwork.LoadLevel("Room for " + PhotonNetwork.CurrentRoom.PlayerCount);
            }