Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved OnValueChanged firing for all NetworkVariable reference types whenever character moves

Discussion in 'Netcode for GameObjects' started by jackward84, Aug 30, 2023.

  1. jackward84

    jackward84

    Joined:
    Jan 26, 2017
    Posts:
    87
    Seeing some bizarre behaviour that I have no idea how or why it is occurring.

    Basically I have this:

    Code (CSharp):
    1. public class Something : INetworkSerializable
    2. {
    3.     public int x;
    4.     public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
    5.     {
    6.         serializer.SerializeValue(ref x);
    7.     }
    8. }
    9.  
    10. public class Mobile : NetworkBehaviour
    11. {
    12.     public NetworkVariable<Something> Something = new NetworkVariable<Something>();
    13.  
    14.     public override void OnNetworkSpawn()
    15.     {
    16.          if(IsServer)
    17.          {
    18.               Something.Value = new(){ x = 5 };
    19.          }
    20.          Something.OnValueChanged += (o, n) => Debug.Log("Something changed!");
    21.     }
    22. }
    23.  
    The problem I'm having, is whenever the Mobile transform position moves, `Something.OnValueChanged` is firing, and I get the output "Something changed!" (on clients only). This can be as simple as just changing the position in the transform of the Mobile. This is not happening for non reference types, like NetworkVariable<bool>.

    I would have expected Something.OnValueChanged would only fire if a field within the class instance changed value - eg: "x". Am I missing something here?
     
  2. jackward84

    jackward84

    Joined:
    Jan 26, 2017
    Posts:
    87
    Kind of answered my own question after finding out the behaviour doesn't happen when implementing IEquatable.