Server (Host) not allowed to write NetworkVariables

So, I have a NetworkVariable for each NetworkPlayer, I initialize it like this:

MyNetworkedVar = new NetworkVariable<short>(INITIAL_VALUE, NetworkVariableReadPermission.Everyone, NetworkVariableWritePermission.Owner);

And supposedly, per the documentation, even when the WritePermission is set to Owner, the Server is still able to write the variable, is that right?

source for that: NetworkVariables | Unity Multiplayer Networking

Since Netcode for GameObjects uses a server authoritative model, the server will always have read or write permissions to any NetworkVariable. As an example, if you set both the read and write permissions to Owner the server can still read and write to the NetworkVariable.

But still, when the server tries to set the variables value like:

MyNetworkedVar.Value = value;
It fails with the following exception:
InvalidOperationException: Client is not allowed to write to this NetworkVariable

Am I missing something? I made sure that the code is executing in the server, and only in the server. Writing from the owner client works fine.

The documentation is incorrect, the server doesn’t have authority over network variables with client owner write permission. I’ve mentioned this previously and was told this is to avoid concurrency issues with server/host and client both changing the same value. Referring to the server as a ‘Client’ doesn’t make much sense though in a server-only scenario.

You can get around the problem by quick switching object ownership to the server but it’s probably not recommended.

3 Likes

Okay I see, I solved it by just making it owned by the server and having the client modify it through ServerRPCs, I guess I trusted the documentation blindly, thanks for the input!

There was a request to update the documentation, maybe that part was missed. I’ve put in a new request to have it updated.

1 Like

Looks like the request was addressed in this PR a couple weeks ago, but for some reason it didn’t go live :eyes:
The docs team is looking into that!

2 Likes

Looks like it is in the doc now :smile:

1 Like

\o/

1 Like