Player Spawning Twice?

Options
I am just starting with Photon and I followed the basic tutorials from the PUN tutorials and I am having a small issue where when another player joins my game both the master and the joined player spawn twice. I am not totally sure why they are doing this twice, I do see where I have different instantiate lines in different codes, but when I try to comment one out I get errors. Can anyone point me in the right direction? I'll post my code.

This is my Launcher code:
[CODE]using UnityEngine;
using UnityEngine.SceneManagement;

namespace Com.MyCompany.MyGame
{
public class Launcher : Photon.PunBehaviour, IPunCallbacks
{
#region Public Variables
public byte MaxPlayersPerRoom = 4;

public PhotonLogLevel Loglevel = PhotonLogLevel.Informational;

public GameObject controlPanel;
public GameObject progressLabel;
#endregion


#region Private Variables
bool isConnecting;

string _gameVersion = "1";
#endregion


#region MonoBehaviour CallBacks
void Start()
{
progressLabel.SetActive(false);
controlPanel.SetActive(true);
}

void Awake()
{
PhotonNetwork.logLevel = Loglevel;

PhotonNetwork.autoJoinLobby = false;

PhotonNetwork.automaticallySyncScene = true;
}
#endregion


#region Public Methods
public void Connect()
{
isConnecting = true;

progressLabel.SetActive(true);
controlPanel.SetActive(false);

if (PhotonNetwork.connected)
{
PhotonNetwork.JoinRandomRoom();
}
else
{
PhotonNetwork.ConnectUsingSettings(_gameVersion);
}
}

public void SinglePlayerConnect()
{
SceneManager.LoadScene("StartArea");
}
#endregion


#region Photon.PunBehaviour CallBacks
public override void OnConnectedToMaster()
{
if (isConnecting)
{
PhotonNetwork.JoinRandomRoom();
}
}


public override void OnDisconnectedFromPhoton()
{
progressLabel.SetActive(false);
controlPanel.SetActive(true);
}
#endregion


public override void OnPhotonRandomJoinFailed(object[] codeAndMsg)
{
PhotonNetwork.CreateRoom(null, new RoomOptions() { MaxPlayers = 4 }, null);
}

public override void OnJoinedRoom()
{
if (PhotonNetwork.room.PlayerCount == 1)
{
PhotonNetwork.LoadLevel("StartArea");
}
}
}
}[/CODE]

and this is on my Game Manager:
[CODE]using UnityEngine;
using UnityEngine.SceneManagement;


namespace Com.MyCompany.MyGame
{
public class GameManager : Photon.PunBehaviour
{
public GameObject playerPrefab;

void Start()
{
if (playerPrefab != null)
{
PhotonNetwork.Instantiate(this.playerPrefab.name, new Vector3(0f, 5f, 0f), Quaternion.identity, 0);
}
}

public override void OnPhotonPlayerConnected(PhotonPlayer other)
{
if (PhotonNetwork.isMasterClient)
{
LoadArena();
}
}

public override void OnPhotonPlayerDisconnected(PhotonPlayer other)
{
if (PhotonNetwork.isMasterClient)
{
LoadArena();
}
}

public override void OnLeftRoom()
{
SceneManager.LoadScene(0);
}

public void LeaveRoom()
{
PhotonNetwork.LeaveRoom();
}

void LoadArena()
{
if (PhotonNetwork.isMasterClient)
{
PhotonNetwork.LoadLevel("StartArea");
}
}
}
}[/CODE]

Any help at all would be much appreciated!

Comments

  • CorvaNocta
    edited March 2018
    Options
    I did some poking around with my code and still haven't found an answer. I'm confused because I literally copied and pasted this code from Photom's own tutorial, amd evem there I had the same issue. Anyone have any thoughts on a fix? Is there a better tutorial I should be using?
  • CorvaNocta
    Options
    Doing some more poking around, I tried moving the Instantiate line to different places in the script to see if that would help. Unfortunately, if that line of code exists anywhere except the Start function, no player spawns and the camera simply zooms away from my map at ridiculous speeds. Every time I have another player join the game, both the Player and the Master spawn twice, also regardless of where the Master is, that player returns to point 0, 0, 0 and then spawns again. No idea why that happens either. I can't find any code that suggests it would clash with what I have currently.

    I am also a little concerned that I have yet to get a response, or am able to google how others have fixed this problem. I'm clearly not the only one with this issue but there seems to be a resounding silence from, well everyone. I mean I get that it has only been 2 days but still, this seems like an issue that shouldn't exist (considering I got the code directly from the Photon tutorials)
  • HI @CorvaNocta,

    it seems that you have multiple calls of PhotonNetwork.LoadLevel("StartArea");. As far as I can see this is called from the OnJoinedRoom callback as well as whenever a another clients joins the same room. Since I guess the GameManager is attached to a game object of that scene, it always calls his Start function where you instantiate a new player prefab each time. The player prefab will always be NULL if the scene is loaded again. Please check how many times the Start function of the GameManager is called.