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

  1. Create a hierarchy (some parents and children) of GameObjects in a scene.
  2. Add NetworkObjects to all of the GameObjects and make sure AutoObjectParentSync is on.
  3. Add a NetworkManager to the scene.
  4. Enter play mode.
  5. Start a host.
  6. Stop the host.
  7. 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: image image

After stop and restart of host, ZRailAndLight’s parent is States with ID 44, with hierarchy completely shuffled: image image

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

Most upvoted comments

No problem, happy to help this get solved.

AutoObjectSyncWithRelay.zip

I took your example and changed the following:

  • Added Relay package
  • Changed NGO version to 1.9.1
  • Added hierarchy of NetworkObjects as in my project
  • Added NetworkConnection.cs, taking care of switching between “offline” host and relay host
  • Adjusted NetworkManagerHelper to show buttons for switching between offline and relay
  • I didn’t do/add any parenting myself in these scripts.

To reproduce the problem:

  • Expand (Alt+click) the full hierarchy starting at the “Machine” GameObject
  • Start play mode
  • Switch to relay with the OnGUI button
  • Observe change in hierarchy starting at “Machine” GameObject AutoObjectSyncParentBug

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).