diff --git a/com.unity.netcode.gameobjects/Runtime/Core/FindObjects.cs b/com.unity.netcode.gameobjects/Runtime/Core/FindObjects.cs index 59329f8a81..109d40f4d3 100644 --- a/com.unity.netcode.gameobjects/Runtime/Core/FindObjects.cs +++ b/com.unity.netcode.gameobjects/Runtime/Core/FindObjects.cs @@ -69,8 +69,8 @@ private struct ObjectsInSceneEnumerator : IEnumerable, IEnumerator wher internal ObjectsInSceneEnumerator(Scene scene, bool includeInactive) { m_IncludeInactive = includeInactive; - - m_RootObjects = scene.GetRootGameObjects(); + // If the scene is invalid, use an empty array. + m_RootObjects = scene.IsValid() ? scene.GetRootGameObjects() : new UnityEngine.GameObject[0]; m_RootIndex = 0; m_CurrentChildObjects = null; m_CurrentChildIndex = 0; diff --git a/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs b/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs index 52e4dad4f1..6905caf3b2 100644 --- a/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs +++ b/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs @@ -2971,17 +2971,31 @@ internal void SynchronizeOwnerNetworkVariables(ulong originalOwnerId, ulong orig childBehaviour.MarkOwnerReadDirtyAndCheckOwnerWriteIsDirty(); } + // If the spawn authority of a distributed authority network topology has invoked a change in ownership, + // then we want to invoke the NetworkBehaviourUpdate prior to changing the owner back. + if (NetworkManager.DistributedAuthorityMode) + { + // Force send a state update for all owner read NetworkVariables and any currently dirty + // owner write NetworkVariables. + NetworkManagerOwner.BehaviourUpdater.NetworkBehaviourUpdate(true); + } + // Now set the new owner and previous owner identifiers back to their original new values - // before we run the NetworkBehaviourUpdate. For owner read only permissions this order of - // operations is **particularly important** as we need to first (above) mark things as dirty - // from the context of the original owner and then second (below) we need to send the messages - // which requires the new owner to be set for owner read permission NetworkVariables. + // after we run the NetworkBehaviourUpdate. OwnerClientId = currentOwnerId; PreviousOwnerId = originalOwnerId; - // Force send a state update for all owner read NetworkVariables and any currently dirty - // owner write NetworkVariables. - NetworkManagerOwner.BehaviourUpdater.NetworkBehaviourUpdate(true); + // For owner read only permissions this order of operations is **particularly important** as + // we need to first (above) mark things as dirty from the context of the original owner and + // then second (below) we need to send the messages which requires the new owner to be set. + // Note: Owner read only NetworkVariables do not make sense in a distributed authority topology + // since the only instance that can write is the owner. + if (!NetworkManager.DistributedAuthorityMode) + { + // Force send a state update for all owner read NetworkVariables and any currently dirty + // owner write NetworkVariables. + NetworkManagerOwner.BehaviourUpdater.NetworkBehaviourUpdate(true); + } } // NGO currently guarantees that the client will receive spawn data for all objects in one network tick.