[Resolved] Issue enabling/disabling components using RPC

Options
I am having issues enabling/disabling a GameObjects components using RPCs. The RPC call works fine on the local client, however, the remote client does not see the change other than the bullet spawn point changing which is due to the fact that a bullet prefab is instantiated via an RPC call. Am I mistaken that the RPC will fire on the gameobject from which it is called on the remote host? Or do I need to tell the remote host which player the weapon is attached to?

Both of the following scripts are attached to the players.

Weapon Swap controller class.
[code2=csharp]// Update is called once per frame
void Update()
{
// Return if not the controlling player
if (!photonView.isMine)
{
return;
}

if (Input.GetButtonDown("Weapon1"))
{
if (!primaryWeapon.enabled)
{
Debug.Log("Setting primary");
// Tell others of my change
photonView.RPC("SetCurrentWeapon", PhotonTargets.All, true);
}
}
else if (Input.GetButtonDown("Weapon2"))
{
if (!secondaryWeapon.enabled)
{

Debug.Log("Setting secondary");
// Tell others of my change
photonView.RPC("SetCurrentWeapon", PhotonTargets.All, false);
}
}

}

/**
* Set the current weapon
*/
[RPC]
public void SetCurrentWeapon(bool isPrimary)
{
if (primaryWeapon != null && isPrimary)
{
primaryWeapon.SetState(true, true);
if (secondaryWeapon != null)
{
secondaryWeapon.SetState(false, false);
}

currentWeapon = primaryWeapon;
}
if (secondaryWeapon != null && !isPrimary)
{
secondaryWeapon.SetState(true, true);
if (primaryWeapon != null)
{
primaryWeapon.SetState(false, false);
}

currentWeapon = secondaryWeapon;
}
}[/code2]

Individual weapon class:
[code2=csharp]/*
* Determine if gun is currently active in the scene
*/
public void SetState(bool isEnabled, bool isVisible)
{
enabled = isEnabled;

if (isEnabled && isVisible)
{
// Gun is now active do something
}

// Disable components if inactive
foreach (Renderer r in transform.GetComponentsInChildren<Renderer>())
{
Debug.Log(transform.name + " Component " + r.name);
r.enabled = isVisible;
Debug.Log("Setting state for: " + transform.name);
}
}[/code2]

Comments

  • SOLVED

    The issue was that I was grabbing the photonView of the player not the view of the weapon in question. Therefore, to solve the problem I pass in the photonView.viewID of each weapon individually.

    Getting view IDs
    [code2=csharp]int primaryID = primaryWeapon.GetComponent<PhotonView>().viewID;
    int secondaryID = secondaryWeapon.GetComponent<PhotonView>().viewID;

    GetComponent<PhotonView>().RPC("SetCurrentWeapon", PhotonTargets.All, true, primaryID, secondaryID);[/code2]

    New code to update the script to enable/disable the Weapon script and all renderer components.
    [code2=csharp]/**
    * Set the current weapon for all players. Ensure that the viewID matches the object you wish to manipulate!
    */
    [RPC]
    public void SetCurrentWeapon(bool isPrimary, int primaryID, int secondaryID)
    {

    GameObject a = PhotonView.Find(primaryID).gameObject;
    GameObject b = PhotonView.Find(secondaryID).gameObject;
    Weapon aWeap = a.GetComponent<Weapon>();
    Weapon bWeap = b.GetComponent<Weapon>();

    Debug.Log(primaryID + " " + a.name);
    Debug.Log(secondaryID + " " + b.name);

    if (aWeap != null && isPrimary)
    {
    aWeap.SetState(true, true);
    if (secondaryWeapon != null)
    {
    bWeap.SetState(false, false);
    }

    currentWeapon = aWeap;
    }
    if (bWeap != null && !isPrimary)
    {
    bWeap.SetState(true, true);
    if (aWeap != null)
    {
    aWeap.SetState(false, false);
    }

    currentWeapon = bWeap;
    }
    }[/code2]

    The code for SetStatus was not changed.
  • Tobias
    Options
    Thanks for the update and a solution. We appreciate that!
    I'll move this topic to the PUN forum.