photon OnloadedLevel/start called twice on client

OnloadedLevel and start called twice on the client resulting in the creation of an extra player (with no one controlling it) only in the master client
i am instantiating my player on the start of my "WaitRoomManager" like so:

PhotonNetwork.Instantiate(this.playerPrefab.name, new Vector3(0f,0f,0f), Quaternion.identity, 0);

in the master it is only called once but on the client it is called twice.

screenshot:
https://drive.google.com/open?id=0BxjwQw0-H0sldnZ2SzNCdFRIRXc

Comments

  • Hi @yosi1202,

    if you run one of the clients in the Unity Editor you can check which client creates the 'extra' player by taking a look at its PhotonView component and check its owner. This might be helpful solving the problem. Please also make sure that you don't have multiple Instantiation calls. Do you load another scene after instantiating the player? This might also cause some trouble.
  • @Christian_Simon thanks for the reply, i checked and the owner is the client, as i said, i happens because the Start() function of my "WaitRoomManager" script is called twice for the client for some reason...
    also i am not loading another scene after instantiating the player and the only Instantiation call is in the Start() function
  • Can you please share the source code of the WaitRoomManager? Under normal circumstances a Start() function isn't called twice or even more.

    Did you already test what happens if you connect a third client to the room? How many objects are being created then? 4, 5 or even 6 as I would guess?
  • @Christian_Simon Strangely when player 3 enter all players now see 4 knight... it go like this:
    player 1 create room -> 1 knight created
    player 2 enter room -> player 2 see 2 knights and player 1 see 3 knights
    player 3 enter room -> all players see 4 knights
    player 4 enter room -> all players see 5 knights
    and so on...

    the code:

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

    public class WaitRoomManager : Photon.PunBehaviour
    {
    static public WaitRoomManager instance;

    private List PlayersList = new List ();

    [SerializeField] GameObject PlayerLinePre;
    [SerializeField] Transform PlayersUi;
    [SerializeField] Button StartButton;
    [SerializeField] GameObject playerPrefab;

    // Use this for initialization
    void Awake ()
    {
    if (instance == null) {
    instance = this;
    }

    if( this != instance ) return;

    PhotonNetwork.automaticallySyncScene = true;

    RefreshPlayersList ();

    if (playerPrefab == null) {
    Debug.LogError("Missing playerPrefab Reference. Please set it up in GameObject 'Game Manager'",this);
    }
    else {
    PhotonNetwork.Instantiate(this.playerPrefab.name, new Vector3(0f,0f,0f), Quaternion.identity, 0);
    print ("Woohoo");
    }
    }

    // Update is called once per frame
    void Update () {

    }

    void OnPhotonPlayerConnected( PhotonPlayer player )
    {
    Debug.Log( "OnPhotonPlayerConnected() " + player.NickName ); // not seen if you're the player connecting
    RefreshPlayersList();
    }

    void OnPhotonPlayerDisconnected( PhotonPlayer player )
    {
    Debug.Log( "OnPhotonPlayerConnected() " + player.NickName ); // not seen if you're the player connecting
    RefreshPlayersList();
    }

    void RefreshPlayersList()
    {
    if (PlayersList.Count > 0) {
    for (int i = 0; i < PlayersList.Count; i++) {
    Destroy (PlayersList [i]);
    }

    PlayersList.Clear ();
    }

    if ((PhotonNetwork.room.PlayerCount >= 2 && PhotonNetwork.isMasterClient) || (PhotonNetwork.room.MaxPlayers == 1 && PhotonNetwork.isMasterClient)) {
    StartButton.interactable = true;
    }

    for (int i = 0; i < PhotonNetwork.room.PlayerCount; i++) {
    GameObject newPlayer = Instantiate (PlayerLinePre);
    newPlayer.transform.SetParent (PlayersUi);
    newPlayer.GetComponent().anchoredPosition = new Vector2 (0, -20 - (i *30));
    newPlayer.GetComponent ().localScale = new Vector3 (1, 1, 1);
    newPlayer.transform.Find ("PlayerName").GetComponent ().text = "Player " + (i + 1).ToString ();
    //newRoom.transform.Find ("PlayersCount").GetComponent().text =
    // PhotonNetwork.GetRoomList()[i].PlayerCount.ToString() + "/" + PhotonNetwork.GetRoomList()[i].MaxPlayers.ToString();
    //newRoom.transform.Find ("Join").GetComponent ().onClick.AddListener ( () => { JoinRoom(newRoom.transform.Find ("Room Name").GetComponent().text); } );
    PlayersList.Add (newPlayer);
    }
    }

    public void StartGame()
    {
    if (PhotonNetwork.isMasterClient) {
    PhotonNetwork.room.IsOpen = false;
    PhotonNetwork.room.IsVisible = false;
    PhotonNetwork.LoadLevel ("MazePvP");
    }
    }

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

    public void LeaveRoom()
    {
    PhotonNetwork.LeaveRoom();
    }
    }
  • I did a few tests with your script. Sadly I wasn't able to reproduce the described behaviour, everything worked fine for me. Can you maybe share me a repro project so I can take a look at it in more detail?
  • yosi1202
    yosi1202
    edited May 2017
    repro project? you mean just send the entire project?
    can it be a problem with the scene?
  • Well the entire project might be a bit over the top, but a project where this issue occurs and I can investigate. I don't know how many assets you currently have in your project. If you don't have that many assets in the project you can also share the complete project with me, otherwise just try to remove those objects which are not important for testing if possible.

    Then upload the zipped project - Dropbox or Google Drive would be nice - and send me the download link, either via private message here or via mail at developer@photonengine.com (in this case please mention this topic and ask to forward this to me).
  • Hi @yosi1202,

    thank you for sharing the project with me. I have taken a look into it and found the issue, which is inside your PhotonNetworkManager script, in detail it is located in the OnJoinedRoom callback. There is currently no problem for the MasterClient, but when the second clients joins and this callback is processed, the client has loaded the scene from the received custom room properties ('WaitRoom' in this case) and loads the same scene again afterwards because LoadLevel(...) is called. To fix this you can add a condition before the LoadLevel(...) call, like this:
    void OnJoinedRoom()
    {
        Debug.Log("Joined Room");
            
        if (PhotonNetwork.isMasterClient)
        {
            PhotonNetwork.LoadLevel("WaitRoom");
        }
    }
    This prevents non-MasterClients from loading the scene twice, which currently happens as far as I noticed. Please let me know if the issues still appears after changing this. It didn't occur again during my tests.
  • Ho man :s i cant believe that was it, I was so focused in the "WaitRoomManager" that I didn't noticed it,
    thank you so much it solved it