RPC not syncing object status

Options
Hi all,
I am new to Photon/RPC and I got this noob issue below.
on my first scene, after the player is instantiated, I also instantiate his weapon on a separate script.
The problem is that only the local player can see the weapon. The other players can't see it although I am calling the RPC method to broadcast this weapon instantiation.

Does anyone know the issue here? THanks a lot
void Start()
    {
        if(photonView.IsMine)
        transform.GetComponent<PhotonView>().RPC("SimpleGun", RpcTarget.AllBuffered);
    }
 
    [PunRPC]
    void SimpleGun()
    {
        weaponContainer = GameObject.Find("WeaponContainer");
        primaryWeapon = Instantiate(Resources.Load("machineGun"), weaponContainer.transform.position, weaponContainer.transform.rotation) as GameObject;
        primaryWeapon.transform.parent = weaponContainer.transform;
    }

Answers

  • S_Oliver
    S_Oliver ✭✭✭
    Options

    If every Player has a GameObject called WeaponContainer, this would not work since GameObject.Find will always find the same. Maybe thats the problem.

  • tomahawk
    Options
    thanks for the feedback Oliver,
    I modified the weaponContainer to get the value from this function below:

    hence, weaponContainer = GetLocalWeaponContainer(), but still does not work :(
    GameObject GetLocalWeaponContainer()
        {
            GameObject[] weaponContainers = GameObject.FindGameObjectsWithTag("WeaponContainer");
            foreach (GameObject weaponContainer in weaponContainers )
            {
                if (weaponContainer .GetComponent<PhotonView>().IsMine)
                {
                    return weaponContainer;
                }
            }
            return null;
        }
    
  • S_Oliver
    S_Oliver ✭✭✭
    Options

    Why you not just make a field an drag n drop the Container into the slot on the inspector?

  • tomahawk
    tomahawk
    edited April 2020
    Options
    Nice tip! implemented :)

    anyhow, each player on the scene (2 for example) are still unable to see each others weapon. Find the full code below...

    (with this code below I am getting an error on the "primaryWeapon.SetActive" line, but does not make sense for me as the prefab was instantiated...
    UnassignedReferenceException: The variable primaryWeapon of WeaponSetup has not been assigned.
    You probably need to assign the primaryWeapon variable of the WeaponSetup script in the inspector.
    public Transform weaponContainer;
        public GameObject primaryWeapon;
    
        void Start()
        {
            PlayerSelection();
            if (photonView.IsMine)
                transform.GetComponent<PhotonView>().RPC("SimpleGun", RpcTarget.AllBuffered);
        }
    
        [PunRPC]
        void SimpleGun()
        {
            primaryWeapon.SetActive(true);
        }
        
        void PlayerSelection()
        {
            primaryWeapon = Instantiate(Resources.Load("machineGun"), weaponContainer.transform.position, weaponContainer.transform.rotation) as GameObject;
            primaryWeapon.transform.parent = weaponContainer.transform;
            primaryWeapon.SetActive(false);
    
        }
    
  • AMRO
    Options
    Any luck? I'm trying to figure this out as well.
  • Is the PlayerSelection() called for everyone in every client?
    It looks like the weapon is created locally, as you are not PhotonNetowrk.Instantiate().
  • Alhammadi
    Options
    you should use PhotonNetwork.Instantiate instead of Instantiate. this should solve your problem