Skip to content

Commit 2dd2918

Browse files
fix
Fixing some issues based on varying destroy order of operations (more recent change where instantiation order does not reflect the order in which they will be destroyed via SceneManager during an in-session scene load (single mode).
1 parent 296b9f5 commit 2dd2918

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

com.unity.netcode.gameobjects/Runtime/Components/Helpers/AttachableBehaviour.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,8 @@ internal void ForceComponentChange(bool isAttaching, bool forcedChange)
392392

393393
foreach (var componentControllerEntry in ComponentControllers)
394394
{
395-
if (componentControllerEntry.AutoTrigger.HasFlag(triggerType))
395+
// Only if the component controller still exists and has the appropriate flag.
396+
if (componentControllerEntry.ComponentController && componentControllerEntry.AutoTrigger.HasFlag(triggerType))
396397
{
397398
componentControllerEntry.ComponentController.ForceChangeEnabled(componentControllerEntry.EnableOnAttach ? isAttaching : !isAttaching, forcedChange);
398399
}
@@ -459,7 +460,14 @@ internal void InternalDetach()
459460
{
460461
if (m_AttachableNode)
461462
{
462-
if (m_DefaultParent)
463+
// TODO-FIX: We might track if something has been "destroyed" in order
464+
// to be able to be 100% sure in the event a user disables the world item
465+
// when detatched. Otherwise, we keep this in place and make note of it
466+
// in documentation.
467+
// Issue:
468+
// Edge-case where the parent could be in the middle of being destroyed.
469+
// If not active in the hierarchy, then don't attempt to set the parent.
470+
if (m_DefaultParent && m_DefaultParent.activeInHierarchy)
463471
{
464472
// Set the original parent and origianl local position and rotation
465473
transform.SetParent(m_DefaultParent.transform, false);

com.unity.netcode.gameobjects/Runtime/Components/Helpers/AttachableNode.cs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,36 @@ public override void OnNetworkPreDespawn()
7171
{
7272
for (int i = m_AttachedBehaviours.Count - 1; i >= 0; i--)
7373
{
74-
if (!m_AttachedBehaviours[i])
74+
var attachable = m_AttachedBehaviours[i];
75+
if (!attachable)
7576
{
7677
continue;
7778
}
7879
// If we don't have authority but should detach on despawn,
7980
// then proceed to detach.
80-
if (!m_AttachedBehaviours[i].HasAuthority)
81+
if (!attachable.HasAuthority)
8182
{
82-
m_AttachedBehaviours[i].ForceDetach();
83+
attachable.ForceDetach();
8384
}
8485
else
8586
{
86-
// Detach the normal way with authority
87-
m_AttachedBehaviours[i].Detach();
87+
// TODO-FIX: We might track if something has been "destroyed" in order
88+
// to be able to be 100% sure this is specific to being destroyed.
89+
// Otherwise, we keep this in place and make note of it
90+
// in documentation that you cannot detatch from something already despawned.
91+
// Issue: When trying to detatch if the thing attached is no longer
92+
// spawned. Instantiation order recently changed such that
93+
// the attachable =or= the attach node target could be despawned
94+
// and in the middle of being destroyed. Resolution for this
95+
// is to skip over destroyed object (default) and then only sort
96+
// through the things the local NetworkManager instance has authority
97+
// over. Even then, we have to check if the attached object is still
98+
// spawned before attempting to detatch it.
99+
if (attachable.IsSpawned)
100+
{
101+
// Detach the normal way with authority
102+
attachable.Detach();
103+
}
88104
}
89105
}
90106
}

0 commit comments

Comments
 (0)