How can I change a player's color?

I managed to do a physics based coop prototype using network transforms and network rigidbody components, and it works good. I’m using RCP server calls for the .AddForce functions in FixedDeltaTime.

However, how can I update player A color in realtime, so player B sees it too? I tried doing a Server RCP call, but that didn’t work, it only works if I do it locally in the update. Can someone please guide me with what client/server functions do I need? Thanks in advance!

You can use a NetworkVariable field and change the player’s colour when its value changes.

    [SerializeField] private NetworkVariable<Color> color = new NetworkVariable<Color>();

    private void Awake()
    {
        color.OnValueChanged += OnColorChanged;
    }

    private void OnColorChanged(Color previousValue, Color newValue)
    {
        // change player colour
    }
1 Like

Hi there! Thank you for your reply, the code works, however, only the host is able to perform the color change. The client can see it from the host, but the client cannot do it

InvalidOperationException: Client is not allowed to write to this NetworkVariable
Unity.Netcode.NetworkVariable

If you want to keep server authority you can have the client send an rpc requesting the colour change and have the server/host change it.

You can give client authority over changing values but it isn’t always recommended, you may have to put additional checks in place on the server/host that the values are acceptable. For a NetworkVariable you can give the client authority like this:

    [SerializeField] private NetworkVariable<Color> color = new NetworkVariable<Color>(default, NetworkVariableReadPermission.Everyone, NetworkVariableWritePermission.Owner);

Something else I should add is if you do this the server doesn’t have authority to change the value, not in NGO at least.