diff --git a/Assets/Mirror/Runtime/NetworkIdentity.cs b/Assets/Mirror/Runtime/NetworkIdentity.cs index 1984e78d5..4b902791f 100644 --- a/Assets/Mirror/Runtime/NetworkIdentity.cs +++ b/Assets/Mirror/Runtime/NetworkIdentity.cs @@ -971,6 +971,21 @@ internal void AddObserver(NetworkConnection conn) conn.AddToVisList(this); } + // helper function to call OnRebuildObservers in all components + // -> HashSet is passed in so we can cache it! + internal bool GetNewObservers(HashSet observersSet, bool initialize) + { + bool result = false; + observersSet.Clear(); + + foreach (NetworkBehaviour comp in NetworkBehaviours) + { + result |= comp.OnRebuildObservers(observersSet, initialize); + } + + return result; + } + static readonly HashSet newObservers = new HashSet(); /// @@ -983,15 +998,9 @@ public void RebuildObservers(bool initialize) return; bool changed = false; - bool result = false; - newObservers.Clear(); - - // call OnRebuildObservers function in components - foreach (NetworkBehaviour comp in NetworkBehaviours) - { - result |= comp.OnRebuildObservers(newObservers, initialize); - } + // call OnRebuildObservers function in all components + bool result = GetNewObservers(newObservers, initialize); // if player connection: ensure player always see himself no matter what. // -> fixes https://github.com/vis2k/Mirror/issues/692 where a