Instantiating two prefabs instead of one

Hello! I have a problem with instantiating my player's prefab. When script instantiating a client's prefab - everything is ok, but when it tries to instantiate master's prefab, I don't know why, but it instantiate two prefabs instead of one. And I can manage them both.

In my code I instantiate player's prefab for the client and for the master only once, so, because of that I really confused and don't understand what is happening with the instantiating.

Hope for response. Thank you!

Answers

  • Hi
    Share us your code =)
  • using System.Collections;
    using System.IO;
    using UnityEngine;
    using UnityEngine.UI;
    using UnityEngine.SceneManagement;

    public class PlayerNetwork : MonoBehaviour
    {
    private PhotonView PhotonView;
    private PlayerMovement CurrentPlayer;
    private SpawnPosition spawnPosition;
    private Coroutine pingCoroutine;
    private ExitGames.Client.Photon.Hashtable playerCustomProperties = new ExitGames.Client.Photon.Hashtable ();
    private int playersInGame = 0;

    public static PlayerNetwork Instance;
    public string PlayerName { get; private set; }

    public int PlayersInGame
    {
    get {
    return playersInGame;
    }
    set {
    playersInGame = value;
    }
    }

    private void Awake ()
    {
    Instance = this;
    PlayerName = "Player #" + Random.Range (0, 100);
    PhotonView = GetComponent ();
    SceneManager.sceneLoaded += OnSceneFinishedLoading;
    }

    private void Start ()
    {
    PhotonNetwork.sendRate = 80;
    PhotonNetwork.sendRateOnSerialize = 40;
    }

    private void OnPhotonSerializeView (PhotonStream stream, PhotonMessageInfo info)
    {
    if (stream.isWriting)
    {
    stream.SendNext (PlayersInGame);
    }
    else
    {
    PlayersInGame = (int)stream.ReceiveNext ();
    }
    }

    private void OnSceneFinishedLoading (Scene scene, LoadSceneMode mode)
    {
    //Change the way how players can join the game
    //Sync: in LobbyNetwork automaticallySyncScene = true, disable functionality standing below;
    //Delayed: in LobbyNetwork automaticallySyncScene = false, enaable functionality standing below;
    if (scene.name == "Arena")
    {
    if (PhotonNetwork.isMasterClient)
    {
    MasterLoadedGame ();
    }
    else
    {
    NonMasterLoadedGame ();
    }
    }
    }

    private void MasterLoadedGame () //Run the game only when master clicks "Play"
    {
    PhotonView.RPC ("RPC_LoadedGameScene", PhotonTargets.MasterClient, PhotonNetwork.player);
    PhotonView.RPC ("RPC_LoadGameOthers", PhotonTargets.Others);
    }

    private void NonMasterLoadedGame () //Run the game synchronously when client clicks "Play"
    {
    PhotonView.RPC ("RPC_LoadedGameScene", PhotonTargets.MasterClient, PhotonNetwork.player);
    }

    [PunRPC]
    private void RPC_LoadGameOthers ()
    {
    PhotonNetwork.LoadLevel (2); //Loading room
    }

    [PunRPC]
    private void RPC_LoadedGameScene (PhotonPlayer photonPlayer)
    {
    PlayerManagement.Instance.AddPlayerStats (photonPlayer);
    PlayersInGame ++;

    if (PlayersInGame > PhotonNetwork.playerList.Length)
    {
    PlayersInGame = PhotonNetwork.playerList.Length;
    }

    if (PlayersInGame == PhotonNetwork.playerList.Length)
    {
    print ("All players are loaded in scene.");
    PhotonView.RPC ("RPC_CreatePlayer", PhotonTargets.All);
    }
    }

    public void NewHealth (PhotonPlayer photonPlayer, int health)
    {
    PhotonView.RPC ("RPC_NewHealth", photonPlayer, health);
    }

    //This function protects player's health from hacking because the actual health is stored on server
    [PunRPC]
    private void RPC_NewHealth (int health)
    {
    if (CurrentPlayer == null)
    {
    return;
    }

    if (health <= 0)
    {
    PhotonNetwork.Destroy (CurrentPlayer.gameObject);
    }
    else
    {
    CurrentPlayer.playerCurrentHealth = health;
    }
    }

    [PunRPC]
    private void RPC_CreatePlayer ()
    {
    GameObject obj = null;
    spawnPosition = GameObject.Find ("Floor").GetComponent<SpawnPosition> ();
    int randomValue = Random.Range (0, 7);
    obj = PhotonNetwork.Instantiate (Path.Combine ("Prefabs", "Skeleton@Skin"), spawnPosition.spawnPoints[randomValue].transform.position, Quaternion.identity, 0);
    CurrentPlayer = obj.GetComponent ();
    }

    private IEnumerator C_SetPing ()
    {
    while (PhotonNetwork.connected)
    {
    playerCustomProperties ["Ping"] = PhotonNetwork.GetPing ();
    PhotonNetwork.player.SetCustomProperties (playerCustomProperties);

    yield return new WaitForSeconds (3.0f);
    }

    yield break;
    }

    private void OnConnectedToMaster ()
    {
    if (pingCoroutine != null)
    {
    StopCoroutine (pingCoroutine);
    }

    pingCoroutine = StartCoroutine (C_SetPing ());
    }

    private void OnLeftRoom ()
    {
    if (PlayersInGame > 0)
    {
    PlayersInGame --;
    print ("Players in game: " + PlayersInGame);
    }
    else if (PlayersInGame <= 0)
    {
    PlayersInGame = 0;
    }

    }

    /*private void OnPhotonPlayerConnected ()
    {
    if (PlayersInGame < PhotonNetwork.playerList.Length)
    {
    PlayersInGame ++;
    }
    else if (PlayersInGame >= PhotonNetwork.playerList.Length)
    {
    PlayersInGame = PhotonNetwork.playerList.Length;
    }
    }*/

    private void OnPhotonPlayerDisconnected ()
    {
    if (PlayersInGame > 0)
    {
    PlayersInGame --;
    print ("Players in game: " + PlayersInGame);
    }
    else if (PlayersInGame <= 0)
    {
    PlayersInGame = 0;
    }
    }
    }
    ----------------------------------------------------------------------------------------------------------------------------------------------------------
    So, here it is. I know that the code is not perfect because I just started programming, but any way, this is just a prototype and I need to make it work anyway :) So, the problem is when players are spawned, client has only one prefap (it is correct), but the master has two prefabs (which is incorrect, you know). And the spawning of the players is in the method "RPC_CreatePlayer()".
  • M4TT
    M4TT
    edited February 2018
    You followed the distul tutorial, a nice one.
    You just need to set automaticallySyncScene to false, because as you can see in your code when the master start the game he arleady tells to the every photon player to star the game, so uncheck the option above and it should works without conflict
  • I got you :) But the problem is that this option was unchecked all the time :smiley:
  • Hmmm, I'm receiving this type of warnings in the console: "Received RPC ... for viewID 2001 but this PhotonView does not exist! Was remote PV. Owner called. By: 2 Maybe GO was destroyed but RPC not cleaned up". And what I realized is that my master player had viewID 1001, but when I reconnect my real player has the same viewID 1001 but my another prefab has viewID 2001. So i think it is somehow connected with PhotonView Script on my master player.