com.unity.netcode.gameobjects icon indicating copy to clipboard operation
com.unity.netcode.gameobjects copied to clipboard

Nested networkobjects in scene give warnings on client connect

Open daalen opened this issue 3 years ago • 3 comments

When a scene networkobject has a child networkobject, warnings are produced when they are automatically being replicated upon a new client connecting (not for the server): "Cannot find parent. Parent objects always have to be spawned and replicated BEFORE the child" However, as far as I know I have no control over the order in which Unity spawns or replicates networkobjects, do I? It would stand to reason Unity starts at the root and works downward, but this doesn't seem to be the case...?

Incidentally, for troubleshooting parent-child related network errors (like this or "Only the server can reparent NetworkObjects") it would help immensely to know which networkobject is causing the problem, but the errors/warnings often don't specify anywhere in the trace.

daalen avatar Feb 12 '22 21:02 daalen

Is the object you're parenting the player? In my project this was the case.

As a workaround, try instantiating the object, spawning the object, then reparenting to what you want. This worked for me!

Also, possibly related: #2019

andriyDev avatar Jun 15 '22 02:06 andriyDev

No, this is about scene objects, not manually instantiated or spawned objects.

daalen avatar Jun 15 '22 06:06 daalen

Ah sorry I misunderstood. Now I'm even more convinced this is related to #2019, since this seems to be a case where Netcode is struggling to handle objects that are instantiated with a parent, rather than doing transform.parent = ....

Again, a workaround, but I bet it'd work if you had a NetworkBehaviour that set the parent of the nested Networkobject to the root NetworkObject on network spawn (maybe this would even work if they were already attached? Unsure).

andriyDev avatar Jun 15 '22 07:06 andriyDev

@daalen In the up-and-coming update there will be a bunch of parenting fixes in PR-2146. This includes properly synchronizing in-scene placed multi-generation parent-child hierarchies.

NoelStephensUnity avatar Oct 06 '22 02:10 NoelStephensUnity

That sounds good – but do I understand correctly this only works for scene objects, and not for parenting that happens during gameplay (e.g. a player picks up an object that should parent to its hand)?

daalen avatar Oct 06 '22 06:10 daalen

@daalen In the soon to be released update, the ParentSyncMessage will synchronize:

  • WorldPositionStays (i.e. if true then preserve world space or if not preserve local space)
  • Position, Rotation, and Scale of the child after being parented

The idea is that if you have simple objects that can be picked up that don't normally move around in the world when they are not parented (i.e. backpack, weapon, etc.), then you no longer need to place a NetworkTransform on those objects. To get proper offsets when parenting, you can override the OnNetworkObjectParentChanged virtual method, within a NetworkBehaviour derived component belonging to the child NetworkObject, and on the server-side when that is invoked you can set the child NetworkObject's transform values (the OnNetworkObjectParentChanged is invoked just prior to the server building and sending the ParentSyncMessage).

If you wanted to see how this works, you can grab the current develop branch and look at the test project InSceneObjectParentingTest . When you start the host, check the box next to "Load In-Scene Object" to get the object to pickup/drop. The primary code that handles parenting is the ChildObjectScript.cs.

This works with both in-scene placed and dynamically spawned NetworkObjects during runtime. 👍

NoelStephensUnity avatar Oct 06 '22 15:10 NoelStephensUnity

Cool! The objects I was having trouble with did need NetworkTransforms as they move about, but this is still very helpful. Thanks for the extensive answer!

daalen avatar Oct 06 '22 17:10 daalen