Skip to content

Commit 3706834

Browse files
fix
Added internal NetworkObject.ResetOnDespawn which resolves issue where m_LatestParent could still be set if using object pools. It can now also be used to reset anything that needs to be reset at the end of a NetworkObject despawn.
1 parent c2b1a7e commit 3706834

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Runtime.CompilerServices;
5+
using System.Text;
56
using Unity.Netcode.Components;
67
#if UNITY_EDITOR
78
using UnityEditor;
@@ -1959,10 +1960,18 @@ public void Despawn(bool destroy = true)
19591960
{
19601961
behavior.MarkVariablesDirty(false);
19611962
}
1962-
19631963
NetworkManager.SpawnManager.DespawnObject(this, destroy);
19641964
}
19651965

1966+
internal void ResetOnDespawn()
1967+
{
1968+
// Always clear out the observers list when despawned
1969+
Observers.Clear();
1970+
IsSpawned = false;
1971+
DeferredDespawnTick = 0;
1972+
m_LatestParent = null;
1973+
}
1974+
19661975
/// <summary>
19671976
/// Removes all ownership of an object from any client. Can only be called from server
19681977
/// </summary>
@@ -2695,6 +2704,17 @@ internal static void VerifyParentingStatus()
26952704
if (OrphanChildren.Count > 0)
26962705
{
26972706
NetworkLog.LogWarning($"{nameof(NetworkObject)} ({OrphanChildren.Count}) children not resolved to parents by the end of frame");
2707+
if (NetworkLog.CurrentLogLevel <= LogLevel.Developer)
2708+
{
2709+
var builder = new StringBuilder();
2710+
builder.AppendLine("Orphaned Children:");
2711+
foreach (var child in OrphanChildren)
2712+
{
2713+
builder.Append($"| {child} ");
2714+
}
2715+
builder.AppendLine("|");
2716+
NetworkLog.LogWarning(builder.ToString());
2717+
}
26982718
}
26992719
}
27002720
}

com.unity.netcode.gameobjects/Runtime/Spawning/NetworkSpawnManager.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1717,8 +1717,8 @@ internal void OnDespawnObject(NetworkObject networkObject, bool destroyGameObjec
17171717
}
17181718
}
17191719

1720-
networkObject.IsSpawned = false;
1721-
networkObject.DeferredDespawnTick = 0;
1720+
// Reset the NetworkObject when despawned.
1721+
networkObject.ResetOnDespawn();
17221722

17231723
if (SpawnedObjects.Remove(networkObject.NetworkObjectId))
17241724
{
@@ -1730,9 +1730,6 @@ internal void OnDespawnObject(NetworkObject networkObject, bool destroyGameObjec
17301730
RemovePlayerObject(networkObject, destroyGameObject);
17311731
}
17321732

1733-
// Always clear out the observers list when despawned
1734-
networkObject.Observers.Clear();
1735-
17361733
var gobj = networkObject.gameObject;
17371734
if (destroyGameObject && gobj != null)
17381735
{

0 commit comments

Comments
 (0)