diff --git a/Assets/Mirror/Runtime/NetworkServer.cs b/Assets/Mirror/Runtime/NetworkServer.cs index f42eb037f..9c715b72b 100644 --- a/Assets/Mirror/Runtime/NetworkServer.cs +++ b/Assets/Mirror/Runtime/NetworkServer.cs @@ -73,7 +73,7 @@ public static class NetworkServer public static float disconnectInactiveTimeout = 60f; /// - /// cache the Send(connectionIds) list to avoid allocating each time + /// cache the Send(connectionIds) list to avoid allocating each time /// static readonly List connectionIdsCache = new List(); @@ -217,7 +217,7 @@ public static bool RemoveConnection(int connectionId) } /// - /// called by LocalClient to add itself. dont call directly. + /// called by LocalClient to add itself. dont call directly. /// /// internal static void SetLocalConnection(ULocalConnectionToClient conn) @@ -514,16 +514,23 @@ public static void Update() // update all server objects foreach (KeyValuePair kvp in NetworkIdentity.spawned) { - NetworkIdentity identity = kvp.Value; - if (identity != null) + // try to update. + // spawned might have null entries if the user made a mistake. + // + // IMPORTANT: do not check kvp.Value != null. this is way too + // costly due to Unity's custom null check + // instead, catch the exception if it happens. + // (see 4k benchmark). + try { - identity.ServerUpdate(); + kvp.Value.ServerUpdate(); } - else + // spawned list should have no null entries because we + // always call Remove in OnObjectDestroy everywhere. + // but if it does, we should let the user know how it happens + catch (NullReferenceException) { - // spawned list should have no null entries because we - // always call Remove in OnObjectDestroy everywhere. - logger.LogWarning("Found 'null' entry in spawned list for netId=" + kvp.Key + ". Please call NetworkServer.Destroy to destroy networked objects. Don't use GameObject.Destroy."); + Debug.LogWarning("Found 'null' entry in spawned list for netId=" + kvp.Key + ". Please call NetworkServer.Destroy to destroy networked objects. Don't use GameObject.Destroy."); } } } @@ -971,7 +978,7 @@ public static void SetClientNotReady(NetworkConnection conn) } /// - /// default ready handler. + /// default ready handler. /// /// ///