Repository navigation
Description
Description
If you use NetworkObject.TrySetParent() and NetworkTransform.Teleport() multiple times in the same frame and the NetworkTransform is InLocalSpace, the position data will get corrupted and out of sync between clients.
Repro is very straightforward!
ParrelSync with simulated 750ms lag so you can more easily see the problem.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Netcode;
using Unity.Netcode.Components;
public class Bug0000 : NetworkBehaviour
{
public Transform position1; // None of these Transforms should be at world (0,0,0)
public Transform position2;
public Transform position3;
public Transform parent1;
public Transform parent2;
public GameObject prefab;
void SpawnBug()
{
var temp = GameObject.Instantiate(prefab, position1.position, Quaternion.identity) as GameObject;
NetworkObject networkObj = temp.GetComponent<NetworkObject>();
networkObj.SpawnWithOwnership( NetworkManager.ServerClientId );
networkObj.TrySetParent( parent1 );
Bug0000Move(networkObj, position2.position);
networkObj.TrySetParent( parent2 );
Bug0000Move(networkObj, position3.position);
}
void Bug0000Move(NetworkObject networkObj, Vector3 pos)
{
NetworkTransform netTransform = networkObj.GetComponent<NetworkTransform>();
Debug.Assert(netTransform.InLocalSpace);
var newPos = netTransform.transform.parent.InverseTransformPoint(pos);
netTransform.Teleport(newPos, networkObj.transform.rotation, networkObj.transform.localScale);
}
}
The spawned object will end up in the incorrect position on the screen on the non-host client, and the incorrect position will persist indefinitely until the object is moved.
It appears to me that NetworkTransform:AdjustForChangeInTransformSpace() -- called via NetworkTransform:UpdateInterpolation () via NetworkTransform:OnUpdate() -- calculates an incorrect m_InternalCurrentPosition and therefore ApplyAuthoritativeState() moves the object to the wrong position.
If you run this repro case with the spawned object owner instead being the non-host, the same bug occurs but it resolves itself with an interpolation rather than persisting indefinitely.
Environment
OS: Windows 11
Unity Version: 6000.1.3f1
Netcode Version: 2.4.2
Netcode Topology: Client-Server
Activity
zachstronaut commentedon Sep 5, 2025
I suspect this is somehow a cousin of one and/or both of these:
#3645
#3646
NoelStephensUnity commentedon Sep 8, 2025
@zachstronaut
Ok... my initial gut reaction was "why would you be parenting and then re-parenting within the same callstack"... so I had to circle back to this after having thought about some of your projects...
Just to make sure I am understanding the "real-world" scenario you have captured in this replication script... 😄
Is the real-world issue pertaining to some kind of lowest generation child invocation conundrum where a derived class is changing the parenting but somewhere in the upper-generation child derivations it is also defaulting to changing the parenting (but to a different NetworkObject)....which the lowest generation child would need to invoke the base.OnNetworkSpawn method first and then apply its own parenting?
Or something like that?
(Trying to understand the actual real-world replication of this issue)
Regarding the script:
My assumption is that parent1 & parent2 are transforms of already spawned NetworkObjects?
As a side note (which may be pertinent): Have you had a chance to play around with AttachableBehaviour and/or ComponentController yet?
NoelStephensUnity commentedon Sep 8, 2025
Regarding AttachableBehaviour documentation:
Just realized the documentation needs some adjusted language. It provides a scenario where a player is picking something up... but it is not limited to just players and the combination of AttachableBehaviours, AttachableNodes, and ComponentControllers allows you to create "lego-like" prefabs.
The example in the documentation provided assumes "motion" comes with animation that is synchronized when in reality you could have something like:
Thing to attach to:
Thing to attach:
So, when attached:
Then the authority of the GameObject-A could move around the AttachPoint (local space relative) and the ThingToAttach would move with it and synchronize that way on all clients.
Alternatively, if your project supported user mods you could provide an API to derived classes from AttachableBehaviour and ComponentController where the users of your product could create "custom things" that can be attached to a preset list of "base things" (your project) where the user defined "custom things" could have unique NetworkBehaviour scripts and the like. The minimal cost of an attachable "thing" is that when it is spawned and attached, its GameObject root instance remains instantiated and the associated NetworkObject still handles the routing of messages to the attachable "thing" (i.e. a single GameObject instance with a NetworkObject component remains in the scene hierarchy for each spawned attachable "thing" that has been attached to some other base spawned object).
NoelStephensUnity commentedon Sep 9, 2025
After looking at the script provided I am wondering why you would want to send two, back-to-back, parenting messages after a NetworkObject has been spawned within the same frame/call-stack?
From an NGO messaging perspective, this is what is happening (if spawned on the server side):
Server Side:
...
Client Side
... Client latency + ~1 network tick later (relative to when the server spawned the object).
Which if the NetworkTransform is using LocalSpace, then the over-all net expected position of the spawned object should be the local space position3.position relative to parent2...and the previous parent1 parenting and the move prior to it are irrelevant.
The first thing I would recommend (if possible) is to make an adjustment to the object being spawned NetworkTransform and make that a custom NetworkTransform. The second thing I would do is calculate the final position offset you want just using transforms and no parenting (since the 1st parenting has no real impact on the 2nd with the scenario you provided).
It would look something like this:
Where your script you provided would look more like this:
Otherwise, you will definitely run into issues trying to immediately apply transform state multiple times within the same call stack and method.
zachstronaut commentedon Sep 9, 2025
Thanks @NoelStephensUnity for looking into this and providing some suggestions. I can't analyze them at the moment, but I will look at them tomorrow (Tues) to see if I can apply them.
As for your question:
I'm working on a large, complex game. It spawns player avatars at different door positions which can be under different parents. But also there is save state, so potentially instead of a spawn at a door you spawn in a deeper gameplay state further along in the level. Additionally, players can be on moving objects like elevators and such. This means when players spawn they could need to be parented to the default spawn position, or save state could indicate they need to be parented elsewhere and that elsewhere could happen to be on top of a moving platform, in which case re-parenting needs to happen yet again. We don't serialize that parentage onto those moving things into game save data because it can be derived from what we do save: the spawn world position. And because it is a big complex game, there's a spawn system, and a door system, and a save state restore system, and a dynamic-what-the-hell-are-you-standing-on-and-does-it-move system. So that first frame upon spawn is conditionally flowing through all/some/none of those to arrive at the final parent and final world position.
And yeah I can make an arbitrator that watches all this happen and then applies one parent operation and one position operation at the end of all that flow. Or I could arbitrarily introduce a frame delay between these systems (although, maybe it would have to be a Tick delay?). In some cases I have already even made my own NetworkTransform replacement that handles parenting internally and avoids NetworkObject's parenting methods. (I am currently on the fence about whether I just go all in on that custom replacement and stop trying to use Netcode's provided position sync and parent sync at all.)
But, I really think that re-parenting and re-positioning (interpolated and teleport) are so fundamental to a net library that they ought to be idiot proof and battle hardened. I'd be a bit bummed to end up shipping this game not using the library provided methods for parenting and positioning. I'd love to get upstream improvements for such core functionality as they come along, and as I work on other games, and not find myself forking too much.
I keep running into problems with re-parenting and re-positioning where the operations either fail or there is an interpolation when there should not be. This class of bug with Netcode has been an ongoing theme for me over the past year and a half or so. I suspect the root of the problem is the separation of NetworkObject and NetworkTransform because obviously local coordinate position sync and parent sync go hand-in-hand. A great many (nearly all) of these bugs have been fixed (probably by you, thanks!) -- and I really think this one and #3645 and #3646 ought to be fixed too and not left to the end user to try to mitigate.
Which is a long way of saying that I don't really think it matters why I would do something so silly as re-teleport and re-parent multiple times in a row on the same frame as spawn. It is something that can happen, so it should either be explicitly not supported and throw an error, or it should work, imho.
zachstronaut commentedon Sep 9, 2025
Sorry, yep! The parents are NetworkObjects that exist in the scene file.
213 remaining items
zachstronaut commentedon Oct 3, 2025
@NoelStephensUnity
That would also make unreliable data work again, and the interpolated movement is sooo much better on bad network connections when using unreliable data.
And then one could get into the fun nitty gritty of finding ways to communicate NetworkObjectId without using 64-bits per Tick, of which I can think of at least a couple.
NoelStephensUnity commentedon Oct 6, 2025
Well, the teleport does indeed get applied as a "non-teleport" state update. The issue resides in what comes next.
Remember... the script for Bug301 has a specific order of operations:
Server:
Client-1:
Issue-A1:
Client will send a teleport position in local space that is relative to parent 1 (while the server is parented under parent 2).
Server will teleport to that local space position relative to parent 2 (first part of the interpolation offset issue)
Issue-A2:
Client will send a NetworkTransformMessage with a local space state update relative to parent-2.
The server then:
What I am thinking could solve the issue is to only track the parent when using SwitchTransformSpaceWhenParented is enabled and teleporting while parented. Then it could look like:
However, that logic could break if you repeated the ClientRpc and server-side parent section of script several times in the same frame....because of the order of operations issues... but also because the motion model is now a "quasi-server & quasi-client" motion model where either side could send completely conflicting messages:
Server
Client
Server
You could repeat that many more times, but the core issue is that the "client motion model" is now a mixed server & client motion model where each side is performing multiple actions and will not be updated on the other side until the messages generated by those actions are received and processed...but if a specific order of operations is required then one side will end up out of synch with the other because both sides could have any number of actions (events and/or messages sent immediately) that proceed tick synchronized state updates.
The recommended approach for this kind of scenario is to either pick a motion model and stick with the motion model...or if you have to break the motion model (i.e. server needs to spawn the player and handles the initial parenting) then you need to be more cautious about the order of operations and/or have some form of flag on a related NetworkBehaviour that signals the client-owner has spawned the instance (i.e. anything that could trigger parenting or the like would not do so until the owner has finished any initializations it needed to do)... or other forms of avoiding this kind of scenario (i.e. with an owner authority motion model it would make more sense to have trigger enter events only register on the owner's instance side as opposed to the server so as to keep the parenting in synch with the authority's/owner's NT state updates).
Hmmm...I will need to contemplate that and whether is makes sense or not once we get this PR finalized.
👍
NoelStephensUnity commentedon Oct 7, 2025
Well, it would only assure that a local space teleport NT-State update would apply the state update relative to the current parent (even if the current parent has changed). However, it wouldn't fix the over-all issues (relative to the related issues discovered in this thread) that can occur (as you have demonstrated in this issue).
Netcode for GameObjects current weakness is that it depends upon reliable sequenced messages to preserve order of operations. The option to send messages unreliably (which that too needs better documentation) really is for scenarios where you are less concerned about the order of operations and more concerned about not congesting the reliable sequenced pipeline. Of course, UTP 2.6.0 was released recently that now allows for increasing the number of in-flight reliable messages which, in turn, should help improve delivery when there are many (i.e. 1000+) objects spawned and moving around.
(We are still running tests on this and most likely won't release any update to NGO that allows one to modify this value until at least after the coming update)
Well, we do use bit packing to condense things like NetworkObjectId and NetworkBehaviourId down to 2-3 bytes, but that also requires keeping the default setting of re-using NetworkObjectId's (otherwise it just continually increments which then bit packing becomes less effective at higher numbers...which depending upon the project night not matter as there may never be more than a couple thousand things spawned during the life time of a network session).
zachstronaut commentedon Oct 7, 2025
Do the NGO Messages / NGO Objects panels in the Profiler reflect unpacked or packed size? And if it reflects unpacked size (which I kind of think might be the case) what is the best way to sample actual real traffic usage?
NoelStephensUnity commentedon Oct 7, 2025
The size in the profiler is the message size. As an example, here is where the CreateObjectMessage records the message size.
NetworkTransformMessage does not have a metrics entry (yet) but should be added soon.