Search code examples
c#unity-game-enginephoton

Photon, Disable component on player from another client


I'm trying to disable a light component from another component on a player. I've tried a couple of methods, mostly the same.

void Update()
{
    if (Input.GetKeyDown(KeyCode.F))
    {
        Debug.Log("light");
        transform.GetComponent<PhotonView>().RPC("EnableFlash", RpcTarget.All, gameObject.name);
    }
}

[PunRPC]
void EnableFlash(string name)
{
    Light light = GetComponent<Light>();
    light.enabled = !light.enabled;
}

This is turning my light off, this cannot be seen on other clients for some reason and there are no errors for this piece of code.

Second try:

void Update()
{
    // Torch on//off
    if (Input.GetKeyDown(KeyCode.F))
    {
        pv.RPC("LightToggled", RpcTarget.All, gameObject.name, torchActive);
        torchActive = !torchActive;
    }
}

[PunRPC]
public void LightToggled(string name, bool active)
{
    GameObject g = GameObject.Find(name);
    GameObject light = g.GetComponent<PlayerExtras>().torch.gameObject;
    light.SetActive(active);
}

This does not work either.


Solution

  • As @DigvijaysinhGohil said in the comment under my question said I needed to make sure that both clients had the same name. They did not so to fix this i had to change the name of the player with the script below:

    void Start()
    {
        string name_ = pv.Owner.ToString();
        name_ = name_.Split()[1];
        gameObject.name = name_;
    }
    

    this script changes it from #01 'jerry' to 'jerry'. this changed on all clients and made sure that they are the same name.

    :)