Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved client is not allowed to write to this network variable

Discussion in 'Netcode for GameObjects' started by liambilly, Jul 20, 2023.

  1. liambilly


    May 13, 2022
    altho the code still works im gettin an error "client is not allowed to write to this network variable"
    this is the networkvar with the error;

    NetworkVariable<byte> secCount = new NetworkVariable<byte>(0, NetworkVariableReadPermission.Everyone, NetworkVariableWritePermission.Owner);

    and this is the code where the exception is thown
    [in the second last code statement]

    Code (CSharp):
    1.  [ServerRpc(RequireOwnership = false)]
    2.     private void LoadAmmoServerRpc(int seccount, byte ammo)
    3.     {
    4.         LoadAmmoClientRpc(seccount, ammo);
    5.     }
    6.     [ClientRpc]
    7.     private void LoadAmmoClientRpc(int seccount, byte ammo)
    8.     {
    9.         if (seccount <= 0 || secCount.Value > 0) return;
    10.         ammoChange(ammo);
    11.         secCount.Value = 2;
    12.         MyEvents.UpdateSecAmmoUI?.Invoke(ammo);
    13.     }
  2. jackward84


    Jan 26, 2017
    Your client RPC is being sent to every client, but only the owner has permission to write to it. You want something like:

    Code (CSharp):
    1. if(IsLocalPlayer)
    2. {
    3.     secCount.Value = 2;
    4. }
    My suggestion though is get rid of the owner permission entirely, and instead set the value in your ServerRPC. Currently the client can set secCount to whatever they want, meaning cheaters can and will abuse whatever secCount is.

    It's also unlikely that turning off RequireOwnership on the ServerRPC is what you really want.