Players instantiate all at the same time when UI button pressed

I had trouble messing around with PUN 1 and finally decided that I would start off with an entirely fresh project and I had worked using the documentation on the website. I've run into a problem though, despite keeping things simple and trying to work step by step I've run into a problem where my players aren't instantiating separately and staying separate when they spawn into the scene.

Not only do they instantiate across the network at exactly the same time the clients all copy each other and that means I've got one player controlling all the others across the network, why is this?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

using Photon.Pun;
using Photon.Realtime;

namespace Com.SilverBoarGaming.MultiplayerFPS

{
public class GameManager : MonoBehaviourPunCallbacks

{
public static GameManager Instance;
public GameObject playerPrefab;
public GameObject SpawnUIEmpty;

private void Start()

{
Instance = this;
}



#region Photon Callbacks

public override void OnLeftRoom()

{
SceneManager.LoadScene(0);
}

#endregion

#region Public Methods

public void LeaveRoom()

{
PhotonNetwork.LeaveRoom();
}

#endregion

#region Private Methods

void LoadArena()

{
if (!PhotonNetwork.IsMasterClient)

{
Debug.Log("PhotonNetwork : Trying to Load a level but we are not the master Client");
}

PhotonNetwork.LoadLevel(1);

}

#endregion

#region Photon Callbacks

public override void OnPlayerEnteredRoom(Player other)

{
Debug.LogFormat("OnPlayerEnteredRoom() {0}", other.NickName);

if (PhotonNetwork.IsMasterClient)

{
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();
}

}

#endregion

public void CreatePlayer()

{


if (PlayerManager.LocalPlayerInstance == null)

{
Debug.Log("Player Instantiated");
PhotonNetwork.Instantiate(this.playerPrefab.name, new Vector3(0, 5f, 0f), Quaternion.identity, 0);
SpawnUIEmpty.SetActive(false);

}

else

{
Debug.Log(SceneManagerHelper.ActiveSceneName);
}

}

}

}


Why does the code tag not work very well here? That's weird.

Comments

  • Lethn
    Lethn
    edited May 2019
    I think I've identified the problem code and can at least explain what's going on.


    void LoadArena()

    {
    if (!PhotonNetwork.IsMasterClient)

    {
    Debug.Log("PhotonNetwork : Trying to Load a level but we are not the master Client");
    }

    PhotonNetwork.LoadLevel(1);

    {


    For whatever reason here in this particular if statement when I create a game and have a player join both players are being set to the master when I took away the PhotonNetwork.LoadLevel my second player's screen went black. According to the documentation shouldn't this be preventing that sort of behaviour from happening? I'll experiment more to see if I can find out how to fix the problem myself but it would be nice to know what is actually going on.
  • Right, I can explain the problem in more detail now after some testing, when I create a room and spawn in first a player is created and that all runs fine. However the problems start when I have another player come in, for some reason the code doesn't create a second player like the documentation states it should and interestingly whoever is the host of the server at the time gets the photonview ID that controls the player.