Syncing object color across all players

Options
F123_
F123_

Hi,


I'm creating a multiplayer scenario in unity using the Photon Engine. I wanted to know how can I sync the networked object's color across every user joining the room? I want all players to be able to see the update in the color change when a certain user changes color of the photon object.


Here is my code that I tried:

public PhotonView PV;
private float r, g, b;
private Color color;

// Start is called before the first frame update
void Start()
{
    PV = GetComponent<PhotonView>();
    if (PV.IsMine)
    {
        PV.RPC("ChangeColor", RpcTarget.AllBuffered);
    }
}


[PunRPC]
public void ChangeColor()
{
    r = Random.Range(0f, 1f);
    g = Random.Range(0f, 1f);
    b = Random.Range(0f, 1f);

    color = new Color(r, g, b);

    PV.GetComponent<Renderer>().sharedMaterial.color = color;
}


I used the RPC call to sync the color of the photon object across all players, however I wasnt successful in doing so. I can only change the color of the object individually and the other player does not see the change.


I've tried the solutions some people have posted on other forums but unfortunately it doesnt work for some reason.

I'm new to photon and would appreciate some help, thanks!

Answers

  • Tobias
    Options

    Inherit from MonoBehaviourPun and the script will provide a PhotonView field.

    I hope doing this in Start() is just a way to test the RPC. As Start() is guaranteed to run on all clients anyways, sending an RPC will not gain you much (it will also call a method on all clients).

    You should not buffer RPCs unless that's really needed. In cases like this, I would suggest to send the color as Instantiation Data or set it in a Custom Player Property. Both make it available to joining players but don't store individual update-RPCs.

    You don't send the color to be applied, so it is random on everyone's client, even if the RPC is working.

    Last but not least: I do not understand in which way the RPC doesn't behave what you expect. Maybe add a Debug.Log() call into the ChangeColor RPC method. So you know it's being called.