Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question Where are the NetworkVariableSettings

Discussion in 'Netcode for GameObjects' started by niekzandt, Nov 7, 2021.

  1. niekzandt

    niekzandt

    Joined:
    Sep 2, 2018
    Posts:
    3
    Hey everyone,
    In 1.0.0 the way networkvariables worked was changed. I want to change the write permissions of my NetworkVariables, documentation says I can do this through NetworkVariableSettings like so:
    Code (CSharp):
    1. private NetworkVariable<float> myFloat = new NetworkVariable<float>(new NetworkVariableSettings {WritePermission = NetworkVariablePermission.OwnerOnly}, 5);
    However this doesn't seem to work and it says NetworkVariableSettings doesn't exist. If I look inside of NetworkVariable I can't find anything similar to it either. The documentation was updated on the day of the update. If someone could help me out with this it would be great!
     
  2. luke-unity

    luke-unity

    Unity Technologies

    Joined:
    Sep 30, 2020
    Posts:
    306
    There are no NetworkVariableSettings anymore. Only the server can write to NetworkVariables in the 1.0.0 version.
     
  3. luke-unity

    luke-unity

    Unity Technologies

    Joined:
    Sep 30, 2020
    Posts:
    306
    You are right the docs page is out of date, will update it asap.
     
  4. mateuszwallace

    mateuszwallace

    Joined:
    Dec 13, 2019
    Posts:
    11
    "By default NetworkVariable and its subclasses can only be written to by the server (NetworkVariablePermission.ServerOnly). To change that set the permission to the desired value during initialization:"
    So still the problem exist this documentation have to updated. If in the documentation are more misleading informations creating a multiplayer game with netcode will be nearly impossible.
     
    yacine123 likes this.
  5. nitrogendragon

    nitrogendragon

    Joined:
    Sep 13, 2017
    Posts:
    6
    How do we go about writing to the networkVaraible then when a client clicks on a button for example to change their teamId and update its color? I'm just confused and stressed from trying to get things working so sorry if this was answered already in a different thread. Thanks for any help in advance.
     
  6. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    460
  7. nitrogendragon

    nitrogendragon

    Joined:
    Sep 13, 2017
    Posts:
    6
  8. BinaryVector

    BinaryVector

    Joined:
    Sep 2, 2018
    Posts:
    1
    I am assuming you are also following Dapper Dino's tutorials. I was stuck on the same place, and after realizing that the NetworkVariableSettings have been removed, I tinkered around and ended up with the following code snippet. In my SelectTeam(int userIndex) method, right above I added these lines:

    Code (CSharp):
    1. if(!NetworkManager.Singleton.IsHost)
    2. {
    3.  
    4. if(!NetworkManager.Singleton.LocalClient.PlayerObject.TryGetComponent<ScriptWithServerRpcFunction>(out ScriptWithServerRpcFunction teamChange))
    5. {
    6. return;
    7. }
    8.  
    9. teamChange.SetTeamServerRpc((byte)userTeamIndex);
    10. }
    It worked for 2 clients + 1 host for me. Hopefully this is useful to you. If you are not following that tutorial, the general idea I worked out here was if we are not a host, then can I directly access my local client's components without having to use the ConnectedClients property (in my case that was the error I was getting: "NotServerException: ConnectedClients should only be accessed on the server.").

    Edit: Messed up indentation haha, sorry.
     
    Last edited: Jan 1, 2022
    Mangler64 likes this.
  9. nitrogendragon

    nitrogendragon

    Joined:
    Sep 13, 2017
    Posts:
    6
    Yep, followed dapperdino and now I'm confused because of the updates to netcode. they really need to fix the documentation up so an average human(me) can understand what everything does and doesn't do. Also though don't really get why they removed functionality, its clearly much more complicated to do anything now if my constant failures to update across host and clients is any indication.
     
    BinaryVector likes this.
  10. Mariiooo

    Mariiooo

    Joined:
    Sep 4, 2019
    Posts:
    6

    I have the same problem. I also found in every documentation that you can access the NetworkVariableSettings.. I'm also a newbie.. I hope they fix it soon.
     
  11. Sundiray

    Sundiray

    Joined:
    May 6, 2019
    Posts:
    16
    It is still out of date...
     
  12. afavar

    afavar

    Joined:
    Jul 17, 2013
    Posts:
    48
    The docs are really out of date rn and it is so confusing. There are no NetworkVariableSettings anymore and also no more NetworkVariableInt, NetworkVariableFloat etc. NetworkAnimator component doesn't work with client authority also but this is not stated in the docs. It is really frustrating for the learning process when the docs are misleading people like this.
     
    NMIS_Scott, CosmicStud and jc2 like this.
  13. jc2

    jc2

    Joined:
    Jul 14, 2015
    Posts:
    15
    i agree with afavar, why is this the case with everything in unity?
     
  14. afavar

    afavar

    Joined:
    Jul 17, 2013
    Posts:
    48
    I was using Photon and I wanted to migrate my project to a new networking solution. My first option was to try MLAPI/Netcode. But, as I have been doing the migration, I have realized the docs are kinda mess rn. The lack of information, outdated / misleading content frustrated me. I have continuously failed while trying to implement some of the basic stuff that are mentioned in the docs and I have ended up checking Github for the issues and change logs to try to understand what might be the reason. Here are just two examples;

    NetworkAnimator is one of the first components that people will try at the begining of a project.
    https://docs-multiplayer.unity3d.com/docs/components/networkanimator

    This component has changed a lot with the recent releases. There are only 3 sentences about the component in the docs and the information is not correct. The worst part is, there is an open issue about this on Github almost 3.5 months ago and still there are no changes.

    https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/issues/1348

    NetworkVariable is literally the most important feature when it comes to sync data between clients.
    https://docs-multiplayer.unity3d.com/docs/basics/networkvariable

    Again, many things changed with the recent updates, however, the page on the docs is completely out-of-date rn.
    I believe Netcode really has a potential but things like these are making people stay away from it.

    UPDATE: Netcode and the docs has improved drastically IMO. The docs are much more clear and the content around the general networking concepts are quite good.
     
    Last edited: Mar 15, 2023
  15. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    460
    Yeah it's not a great sign when having good document isn't a priority. I think Unity should be doing a lot more in general to support Netcode users but it seems the powers that be don't see that as a good use of time and resources.

    I was a bit bemused to see their latest survey asking about understanding netcode-related terminology when i think a more important question to ask would be: "How can we improve the Netcode developer experience?".
     
  16. shrdlu518

    shrdlu518

    Joined:
    Mar 27, 2020
    Posts:
    6
  17. CosmoM

    CosmoM

    Joined:
    Oct 31, 2015
    Posts:
    199
  18. elzbeb

    elzbeb

    Joined:
    Jul 30, 2020
    Posts:
    54
    Hello, this did not help fix my issue, could you please provide the whole script where you put this?
    Here is mine

    Code (CSharp):
    1.     public void SetPlayerColor(int colorIndex)
    2.     {
    3.         Debug.Log("CALLED SETPLAYERCCOLOR");
    4.  
    5.         ulong localClientId = NetworkManager.Singleton.LocalClientId;
    6.        
    7.         if(!NetworkManager.Singleton.IsHost)
    8.         {
    9.             if(!NetworkManager.Singleton.LocalClient.PlayerObject.TryGetComponent<PlayerColor>(out PlayerColor teamChange))
    10.             {
    11.                 return;
    12.             }
    13.             teamChange.SetPlayerColorServerRpc((byte)colorIndex);
    14.         }
    15.        
    16.         if (!NetworkManager.Singleton.ConnectedClients.TryGetValue(localClientId, out NetworkClient networkClient))
    17.         {
    18.             return;
    19.         }
    20.        
    21.         if (!networkClient.PlayerObject.TryGetComponent<PlayerColor>(out PlayerColor playerColor))
    22.         {
    23.             return;
    24.         }
    25.        
    26.         playerColor.SetPlayerColorServerRpc((byte)colorIndex);
    27.     }
     
  19. KuPAfoo

    KuPAfoo

    Joined:
    Aug 24, 2013
    Posts:
    17
    Just had to say, this is the best clue there was for this issue.
    Code (CSharp):
    1. "Client is not allowed to write to this NetworkVariableUnity.Netcode.NetworkVariable"
    I was able to resolve with the following code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Unity.Netcode;
    5.  
    6. public class characterCustomizer : MonoBehaviour
    7. {
    8.    
    9.     public void selectAppearance(int hairIndex)
    10.     {
    11.             if(!NetworkManager.Singleton.LocalClient.PlayerObject.TryGetComponent<nakedFeatures>(out nakedFeatures nakedStuff)){
    12.                 return;
    13.             }
    14.             nakedStuff.SetAppearanceServerRpc((byte)hairIndex);
    15.     }
    16. }

    the example should look like this:
    Code (CSharp):
    1. public void SelectTeam(int teamIndex)
    2. {
    3. if(!NetworkManager.Singleton.LocalClient.PlayerObject.TryGetComponent<ScriptWithServerRpcFunction>(out ScriptWithServerRpcFunction teamChange))
    4. {
    5. return;
    6. }
    7. teamChange.SetTeamServerRpc((byte)userTeamIndex);
    8. }