Unity-Technologies / com.unity.netcode.gameobjects

Netcode for GameObjects is a high-level netcode SDK that provides networking capabilities to GameObject/MonoBehaviour workflows within Unity and sits on top of underlying transport layer.
MIT License
2.15k stars 435 forks source link

Only first NetworkTransform synchs when having multiple nested NetworkTransforms on NetworkObject #3002

Closed mixedHans closed 3 months ago

mixedHans commented 3 months ago

Description

I have a player object as well as some multiple network object, that rely on having multiple nested network transforms. A network object can look like this:

When spawning it either as player object as well as normal network object, only the very first NetworkTransform (in my case NetworkTransform1) gets synchronized between every client. When i change the hierarchy to this below, then NetworkTransform2 gets synched.

Reproduce Steps

  1. Create a NetworkObject with at least 2 nested NetworkTransforms
  2. Start 2 instances of the application (Server and client)
  3. Join the same session
  4. Spawn this NetworkObject
  5. Move both objects with a NetworkTransform component on it to another location

Actual Outcome

As owner of the object, both objects move accordingly. As not owner of the objects, only the first NetworkTransform in the hierarchy moves. The other NetworkTransform stays in the place where it has been spawned.

Expected Outcome

Both NetworkTransforms move and synch over all clients.

Environment

Additional Context

I also recognized during debugging, that NetworkObject.NetworkTransforms is null. I dont know why this happens, but i found that if (type.IsInstanceOfType(typeof(NetworkTransform)) || type.IsSubclassOf(typeof(NetworkTransform))) is always false, even when the type is NetworkTransform.

CodeSnipped from NetworkObject class

    m_ChildNetworkBehaviours = new List<NetworkBehaviour>();
    var networkBehaviours = GetComponentsInChildren<NetworkBehaviour>(true);
    for (int i = 0; i < networkBehaviours.Length; i++)
    {
        if (networkBehaviours[i].NetworkObject == this)
        {
            m_ChildNetworkBehaviours.Add(networkBehaviours[i]);
            var type = networkBehaviours[i].GetType();
            if (type.IsInstanceOfType(typeof(NetworkTransform)) || type.IsSubclassOf(typeof(NetworkTransform)))
            {
                if (NetworkTransforms == null)
                {
                    NetworkTransforms = new List<NetworkTransform>();
                }
                NetworkTransforms.Add(networkBehaviours[i] as NetworkTransform);
            }
#if COM_UNITY_MODULES_PHYSICS
            else if (type.IsSubclassOf(typeof(NetworkRigidbodyBase)))
            {
                if (NetworkRigidbodies == null)
                {
                    NetworkRigidbodies = new List<NetworkRigidbodyBase>();
                }
                NetworkRigidbodies.Add(networkBehaviours[i] as NetworkRigidbodyBase);
            }
#endif
        }
    }
mixedHans commented 3 months ago

This issue has been solved within version 2.0.0-pre.4