[SOLVED] Player prefabs only spawning on MasterClient

ginhikari
ginhikari
edited April 2019 in Any Topic & Chat
Hi, I have been trying to spawn players at designated spots and activate the camera only if they are the owner of the prefab. But the player prefabs are only spawning on the MasterClient and I am not too sure why. The debug message within the if statements are returned so I am pretty sure it has nothing to do Actor Number being wrong. What went wrong?
using Photon.Pun;
using Photon.Realtime;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.SceneManagement;

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

    private PhotonView PhotonView;
    private int PlayersInGame = 0;

    private void Awake()
    {
        Instance = this;
        PhotonView = GetComponent<PhotonView>();
        Debug.Log("Awoken");<del class="Delete"></del>
        SceneManager.sceneLoaded += OnSceneFinishedLoading;
    }

    private void OnSceneFinishedLoading(Scene scene, LoadSceneMode mode)
    {
        Debug.Log("Scene Loaded");
        if (scene.name == "multiplayerStage")
        {
            if (PhotonNetwork.IsMasterClient)
            {
                Debug.Log("Master Loaded Game");
                MasterLoadedGame();
            }
        }
        else
        {
            NonMasterLoadedgGame();
        }
    }

    private void MasterLoadedGame()
    {
        PlayersInGame = 1;
        PhotonView.RPC("RPC_LoadedGameScene", RpcTarget.MasterClient);
        PhotonView.RPC("RPC_LoadGameOthers", RpcTarget.Others);
    }

    private void NonMasterLoadedgGame()
    {
        PhotonView.RPC("RPC_LoadedGameScene", RpcTarget.MasterClient);
    }

    [PunRPC]
    public void RPC_LoadGameOthers()
    {
        PhotonNetwork.LoadLevel("multiplayerStage");
    }

    [PunRPC]
    public void RPC_LoadedGameScene()
    {
        PlayersInGame++;
        if(PlayersInGame == PhotonNetwork.PlayerList.Length)
        {
            Debug.Log("All PLAYERS LOADED TO GAME SCENE.");
            PhotonView.RPC("RPC_CreatePlayer", RpcTarget.All);
        }
    }

    [PunRPC]
    public void RPC_CreatePlayer()
    {
        Debug.Log("Spawning Player...");

        Player[] player = PhotonNetwork.PlayerList;
        Debug.Log("Actor number is: "+PhotonNetwork.LocalPlayer.ActorNumber);
        Vector3 VecRotation = new Vector3(128.874f, 178.878f, -180.882f);
        Quaternion rotation = Quaternion.Euler(VecRotation);
        Vector3[] spawnPosition = new[] { new Vector3(-75.9f, 8.57f, -139.2f), new Vector3(14f, 8.57f, -139.2f), new Vector3(91.6f, 8.57f, -139.2f), new Vector3(174.3f, 8.57f, -139.2f) };
        Debug.Log("Spawn init done");



       for (int i = 0; i < player.Length; i++)
        {
            if(PhotonNetwork.LocalPlayer.ActorNumber == i + 1)
            {
                GameObject _playerPrefab = (GameObject)PhotonNetwork.Instantiate("Prefabs/Player", spawnPosition[i], rotation, 0);
                if (PhotonView.IsMine)
                {
                    _playerPrefab.transform.Find("Camera").gameObject.SetActive(true);
                }
            }
            Debug.Log(i);
        }
    }
}

I am also getting this warning.
Received OnSerialization for view ID 1001. We have no such PhotonView! Ignored this if you're leaving a room. State: Joined

Answers

  • So uh, I fixed the problem. There is no bug in the code. It works. I solved it by copying all the game objects from the multiplayerStage scene then paste it in a new scene and it worked because why...? I guess I will never know.