Master client doesn't work

Options
ValenS
ValenS
Hi, i am making a npc random spawn, the problem is if there are 2 players the spawn is multiplied by 2, and if there are 3 the spawn is multiplied by 3. So, if there are a lot of players, a lot of npc are spawning. (sorry i dont speak english).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Realtime;
using Photon.Pun;
using System.IO;
using UnityEngine.UI;
using UnityEngine.UIElements;
using System.Linq;
using TMPro;

public class KidController : MonoBehaviour
{
    public float time;
    public GameObject ayd;

    PhotonView PV;
    Vector3 posR;

    private void Awake()
    {

        PV = GetComponent<PhotonView>();
    }

    private void Update()
    {

        time += 1f * Time.deltaTime;

            if (time >= 2f)
            {

                if (PhotonNetwork.IsMasterClient)
                {

                PV.RPC("InstanciarKid", RpcTarget.All);

                time = 0f;

            } else { return; }
           

            }

    }

    [PunRPC]
    void InstanciarKid()
    {
            posR = new Vector3(
            Random.Range(0, 40),
            0,
            Random.Range(0, 40)
            );
            PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs", "Kid"), posR, Quaternion.identity);

    }

}

Comments

  • Andi
    Andi
    edited February 2021
    Options
    Hi, you have to check if it is the masterClient on your void InstanciarKid(), so before you write anything on that method, do an if statement checking that: if(PhotonNetwork.IsMasterClient()){
    do your stuff}