com.unity.netcode.gameobjects: AutoObjectParentSync for in-scene GameObjects sets wrong parents after stopping and restarting host
Description
Given a hierarchy of GameObjects with NetworkObject components with AutoObjectParentSync on:
- Starting a host assigns every NetworkObject an ID and a parent ID.
- Stopping a host and restarting it seems to assign every NetworkObject a new ID, but does not update parent IDs.
- This results in the hierarchy being shuffled up at random, with high likelihood of orphans.
Reproduce Steps
- Create a hierarchy (some parents and children) of GameObjects in a scene.
- Add NetworkObjects to all of the GameObjects and make sure AutoObjectParentSync is on.
- Add a NetworkManager to the scene.
- Enter play mode.
- Start a host.
- Stop the host.
- Start a host again.
Actual Outcome
The parent child relationships in the scene hierarchy got shuffled.
Expected Outcome
The parent child relationships in the scene hierarchy should not be affected.
Screenshots
Before stop and restart of host, ZRailAndLight’s parent is SubAssemblies with ID 44, and hierarchy is as expected:
After stop and restart of host, ZRailAndLight’s parent is States with ID 44, with hierarchy completely shuffled:
Environment
- OS: Windows
- Unity Version: 2022.3.9f1
- Netcode Version: 1.5.2
- Netcode Commit: ?
Additional Context
My workaround for now is to use this on all NetworkObjects in a NetworkManager.OnClientStopped callback:
public static class NetworkObjectExtensions
{
private static FieldInfo? _latestParentFieldInfo = typeof(NetworkObject).GetField("m_LatestParent", BindingFlags.NonPublic | BindingFlags.Instance);
// Netcode for GameObjects' auto parent sync assigns IDs and parent IDs whenever you start a host.
// When stopping and restarting a host, the object IDs are reassigned, but the parent IDs are kept.
// This messes up our scene hierarchy (IDs pointing to parents are kept, but those parents have new IDs).
// For now, this extension method is a workaround, to be called whenever we close a connection (stop a host).
// Using this, the auto parent sync feature will reevaluate the parent IDs.
public static void ResetParentNetworkObjectId(this NetworkObject networkObject)
{
if (_latestParentFieldInfo == null)
{
Debug.LogError("An update broke our workaround for auto parent sync hierarchy shuffle. Check if bug remains or find new variable name.");
return;
}
_latestParentFieldInfo.SetValue(networkObject, null);
}
}
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 15
No problem, happy to help this get solved.
AutoObjectSyncWithRelay.zip
I took your example and changed the following:
To reproduce the problem:
Was running this with Unity 2022.3.9f1
@ArjanSioux Thanks you for confirming that for me. I marked this for import and will post a PR to resolve this issue. It will land in the update after NGO v1.9.1 (any day this should be available).