diff --git a/Assets/Mirror/Runtime/NetworkConnection.cs b/Assets/Mirror/Runtime/NetworkConnection.cs index 09142dcf7..2a0178bb5 100644 --- a/Assets/Mirror/Runtime/NetworkConnection.cs +++ b/Assets/Mirror/Runtime/NetworkConnection.cs @@ -16,7 +16,7 @@ public class NetworkConnection : IDisposable public string address; public float lastMessageTime; public NetworkIdentity playerController { get; internal set; } - public HashSet clientOwnedObjects; + public HashSet clientOwnedObjects = new HashSet(); public bool logNetworkMessages; // this is always true for regular connections, false for local @@ -59,17 +59,14 @@ public void Dispose() protected virtual void Dispose(bool disposing) { - if (clientOwnedObjects != null) + foreach (uint netId in clientOwnedObjects) { - foreach (uint netId in clientOwnedObjects) + if (NetworkIdentity.spawned.TryGetValue(netId, out NetworkIdentity identity)) { - if (NetworkIdentity.spawned.TryGetValue(netId, out NetworkIdentity identity)) - { - identity.clientAuthorityOwner = null; - } + identity.clientAuthorityOwner = null; } } - clientOwnedObjects = null; + clientOwnedObjects.Clear(); } public void Disconnect() @@ -278,13 +275,12 @@ public virtual bool TransportSend(int channelId, byte[] bytes, out byte error) internal void AddOwnedObject(NetworkIdentity obj) { - clientOwnedObjects = clientOwnedObjects ?? new HashSet(); clientOwnedObjects.Add(obj.netId); } internal void RemoveOwnedObject(NetworkIdentity obj) { - clientOwnedObjects?.Remove(obj.netId); + clientOwnedObjects.Remove(obj.netId); } } } diff --git a/Assets/Mirror/Runtime/NetworkServer.cs b/Assets/Mirror/Runtime/NetworkServer.cs index 38a0bb360..973db9c38 100644 --- a/Assets/Mirror/Runtime/NetworkServer.cs +++ b/Assets/Mirror/Runtime/NetworkServer.cs @@ -977,15 +977,12 @@ public static void DestroyPlayerForConnection(NetworkConnection conn) // so we need null checks for both of them. // => destroy what we can destroy. - if (conn.clientOwnedObjects != null) + HashSet tmp = new HashSet(conn.clientOwnedObjects); + foreach (uint netId in tmp) { - HashSet tmp = new HashSet(conn.clientOwnedObjects); - foreach (uint netId in tmp) + if (NetworkIdentity.spawned.TryGetValue(netId, out NetworkIdentity identity)) { - if (NetworkIdentity.spawned.TryGetValue(netId, out NetworkIdentity identity)) - { - Destroy(identity.gameObject); - } + Destroy(identity.gameObject); } }