Search Unity

NetworkVariable in nested classes

Discussion in 'Netcode for GameObjects' started by GaiusAureliusValeriusDiocletianus, May 25, 2021.

  1. GaiusAureliusValeriusDiocletianus

    GaiusAureliusValeriusDiocletianus

    Joined:
    May 6, 2021
    Posts:
    2
    I had attempted to put my NetworkVariables inside of nested classes, like:

    class Behavior : NetworkBehaviour {
    public class PairOfVariables {
    public NetworkVariableVector3 position = new .... ;
    public NetworkVariableQuaternion rotation = new ... ;
    }
    public class FourPairsOfVariables {
    public PairOfVariables pair1 = new ...;
    ...
    public PairOfVariables pair4 = new ... ;
    }
    public FourPairsOfVariables variables= new ...;
    };

    However, the variables appeared not to be updated on other game instances. I.e. the Server would write the variables, but Clients would not see updates.


    When I un-nested the Variables, and instead put them directly in the Behavior class, like:
    class Behavior : NetworkBehavior {
    public NetworkVariableVector3 variable_pair1_position = new ....;
    public NetworVariableQuaternion variable_pair1_rotation = new ... ;
    ...
    public NetworkVariableQuaternion variable_pair4_rotation = new ... ;
    }

    Clients then began receiving variable updates as expected.

    Is this indended behavior? Is it a bug? Am I missing something else?
     
  2. luke-unity

    luke-unity

    Joined:
    Sep 30, 2020
    Posts:
    306
    This is intended behaviour. We only support NetworkVariables which are direct member fields of the NetworkBehaviour.
     
  3. Hertzole

    Hertzole

    Joined:
    Jul 27, 2013
    Posts:
    422
    Will this limitation be fixed at some point? I can already see it blocking some functionality.
     
  4. luke-unity

    luke-unity

    Joined:
    Sep 30, 2020
    Posts:
    306
    I wouldn't call this a "limitation". If this limitation wasn't there you could do some very weird stuff. For instance two NetworkBehaviours could both share the same class instance in a member field, but they would still be treated as separate variables.

    Do you have an example where this would be blocking functionality?
     
  5. Hertzole

    Hertzole

    Joined:
    Jul 27, 2013
    Posts:
    422
    I just realized I entirely misread the issue. :oops:
    I thought the issue was that you can't use network variables in classes that inherit from another network behavior (which I also hope isn't a limitation). Nevermind me!
     
  6. Alpha7Wolf

    Alpha7Wolf

    Joined:
    Oct 20, 2016
    Posts:
    6
    so there wont be anyway to directly assign NetworkBehavior to the NetworkVar<T> ?