Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

Resolved Server (Host) not allowed to write NetworkVariables

Discussion in 'Netcode for GameObjects' started by Svartskogen, Mar 25, 2023.

  1. Svartskogen

    Svartskogen

    Joined:
    Apr 17, 2017
    Posts:
    9
    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: https://docs-multiplayer.unity3d.com/netcode/current/basics/networkvariable/index.html#permissions

    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.
     
  2. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    658
    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.
     
    codeBatt, Svartskogen and lavagoatGG like this.
  3. Svartskogen

    Svartskogen

    Joined:
    Apr 17, 2017
    Posts:
    9
    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!
     
  4. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    658
    There was a request to update the documentation, maybe that part was missed. I've put in a new request to have it updated.
     
    Svartskogen likes this.
  5. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    440
    Looks like the request was addressed in this PR a couple weeks ago, but for some reason it didn't go live o_O
    The docs team is looking into that!
     
    Svartskogen and cerestorm like this.
  6. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    440
    Svartskogen likes this.
  7. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    658
    \o/
     
    RikuTheFuffs-U likes this.