From 76a0480f52aabac725bd960db1e0f944b9763cd5 Mon Sep 17 00:00:00 2001 From: Noellie Velez Date: Mon, 23 Mar 2026 16:52:46 +0100 Subject: [PATCH 1/6] styling + is spawned check in internal has authory --- .../Runtime/Core/NetworkObject.cs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs b/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs index c667af47db..05df0eabc0 100644 --- a/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs +++ b/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs @@ -390,7 +390,7 @@ private void CheckForInScenePlaced() /// Defaults to true, determines whether the will be destroyed. public void DeferDespawn(int tickOffset, bool destroy = true) { - // Ensure we log the DAMode message first as locking ownership is not allowed if not DA so the DA message is the most relevant. + // The DAMode message is logged first, as ownership locking isn’t allowed when not in DAMode, making it the most relevant message. if (!NetworkManager.DistributedAuthorityMode) { if (NetworkManager.LogLevel <= LogLevel.Error) @@ -606,7 +606,7 @@ internal void RemoveOwnershipExtended(OwnershipStatusExtended extended) /// true or false depending upon lock operation's success public bool SetOwnershipLock(bool lockOwnership = true) { - // Ensure we log the DAMode message first as locking ownership is not allowed if not DA so the DA message is the most relevant. + // The DAMode message is logged first, as ownership locking isn’t allowed when not in DAMode, making it the most relevant message. if (!NetworkManager.DistributedAuthorityMode) { if (NetworkManager.LogLevel <= LogLevel.Error) @@ -1154,8 +1154,11 @@ public bool HasOwnershipStatus(OwnershipStatus status) [MethodImpl(MethodImplOptions.AggressiveInlining)] private bool InternalHasAuthority() { - var networkManager = NetworkManager; - return networkManager.DistributedAuthorityMode ? OwnerClientId == networkManager.LocalClientId : networkManager.IsServer; + if (!IsSpawned) + { + return false; + } + return NetworkManagerOwner.DistributedAuthorityMode ? OwnerClientId == NetworkManagerOwner.LocalClientId : NetworkManagerOwner.IsServer; } /// @@ -3541,7 +3544,7 @@ internal void SceneChangedUpdate(Scene scene, bool notify = false) OnMigratedToNewScene?.Invoke(); // Only the authority side will notify clients of non-parented NetworkObject scene changes - if (isAuthority && notify && !transform.parent) + if (isAuthority && notify && transform.parent == null) { NetworkManagerOwner.SceneManager.NotifyNetworkObjectSceneChanged(this); } From d9c9211a9daffcb2fbe241c1e992d45bb3812c27 Mon Sep 17 00:00:00 2001 From: Noellie Velez Date: Wed, 10 Jun 2026 15:46:04 +0200 Subject: [PATCH 2/6] Use InternalHasAuthority instead of public field --- .../Runtime/Core/NetworkBehaviour.cs | 2 +- .../Runtime/Core/NetworkObject.cs | 28 ++++++++----------- .../DefaultSceneManagerHandler.cs | 4 +-- .../SceneManagement/NetworkSceneManager.cs | 6 ++-- .../Runtime/Spawning/NetworkSpawnManager.cs | 8 +++--- 5 files changed, 21 insertions(+), 27 deletions(-) diff --git a/com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs b/com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs index 16b6d3be62..ab92df14c3 100644 --- a/com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs +++ b/com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs @@ -670,7 +670,7 @@ internal void UpdateNetworkProperties() IsClient = m_NetworkManager.IsListening && m_NetworkManager.IsClient; IsServer = m_NetworkManager.IsListening && m_NetworkManager.IsServer; IsSessionOwner = m_NetworkManager.IsListening && m_NetworkManager.LocalClient.IsSessionOwner; - HasAuthority = m_NetworkObject.HasAuthority; + HasAuthority = m_NetworkObject.InternalHasAuthority(); ServerIsHost = m_NetworkManager.IsListening && m_NetworkManager.ServerIsHost; } } diff --git a/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs b/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs index 47570b74e6..cb0b23006b 100644 --- a/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs +++ b/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs @@ -402,7 +402,6 @@ public void DeferDespawn(int tickOffset, bool destroy = true) { NetworkLog.LogErrorServer($"[{name}] This method is only available in distributed authority mode."); } - return; } @@ -412,11 +411,10 @@ public void DeferDespawn(int tickOffset, bool destroy = true) { NetworkLog.LogErrorServer($"[{name}] Cannot defer despawn while not spawned."); } - return; } - if (!HasAuthority) + if (!InternalHasAuthority()) { if (NetworkManagerOwner.LogLevel <= LogLevel.Error) { @@ -628,12 +626,11 @@ public bool SetOwnershipLock(bool lockOwnership = true) { NetworkLog.LogErrorServer($"[{name}][Attempted Lock While not spawned]"); } - return false; } // If we don't have authority exit early - if (!HasAuthority) + if (!InternalHasAuthority()) { if (NetworkManager.LogLevel <= LogLevel.Error) { @@ -671,7 +668,6 @@ public bool SetOwnershipLock(bool lockOwnership = true) { SendOwnershipStatusUpdate(); } - return true; } @@ -797,7 +793,6 @@ public OwnershipRequestStatus RequestOwnership() { NetworkLog.LogErrorServer($"[{name}][Invalid Operation] Cannot request ownership of an NetworkObject before it is spawned."); } - return OwnershipRequestStatus.InvalidOperation; } // Exit early the local client is already the owner @@ -909,7 +904,7 @@ internal void OwnershipRequest(ulong clientRequestingOwnership) // This action is always authorized as long as the client still has authority. // We need to pass in that this is a request approval ownership change. - NetworkManagerOwner.SpawnManager.ChangeOwnership(this, clientRequestingOwnership, HasAuthority, true); + NetworkManagerOwner.SpawnManager.ChangeOwnership(this, clientRequestingOwnership, InternalHasAuthority(), true); } else { @@ -1158,7 +1153,7 @@ public bool HasOwnershipStatus(OwnershipStatus status) public bool HasAuthority => InternalHasAuthority(); [MethodImpl(MethodImplOptions.AggressiveInlining)] - private bool InternalHasAuthority() + internal bool InternalHasAuthority() { if (!IsSpawned) { @@ -1509,7 +1504,7 @@ public void NetworkShow(ulong clientId) return; } - if (!HasAuthority) + if (!InternalHasAuthority()) { if (NetworkManagerOwner.DistributedAuthorityMode) { @@ -1604,7 +1599,7 @@ public void NetworkHide(ulong clientId) return; } - if (!HasAuthority) + if (!InternalHasAuthority()) { if (NetworkManagerOwner.DistributedAuthorityMode) { @@ -1763,7 +1758,7 @@ private void OnDestroy() { // An authorized destroy is when done by the authority instance or done due to a scene event and the NetworkObject // was marked as destroy pending scene event (which means the destroy with scene property was set). - var isAuthorityDestroy = HasAuthority || NetworkManager.DAHost || DestroyPendingSceneEvent; + var isAuthorityDestroy = InternalHasAuthority() || NetworkManager.DAHost || DestroyPendingSceneEvent; // If the NetworkObject's GameObject is still valid and the scene is still valid and loaded, then we are still valid var isStillValid = gameObject != null && gameObject.scene.IsValid() && gameObject.scene.isLoaded; @@ -2102,7 +2097,7 @@ public void ChangeOwnership(ulong newOwnerClientId) } return; } - NetworkManagerOwner.SpawnManager.ChangeOwnership(this, newOwnerClientId, HasAuthority); + NetworkManagerOwner.SpawnManager.ChangeOwnership(this, newOwnerClientId, InternalHasAuthority()); } /// @@ -2337,7 +2332,7 @@ public bool TrySetParent(NetworkObject parent, bool worldPositionStays = true) // DANGO-TODO: Do we want to worry about ownership permissions here? // It wouldn't make sense to not allow parenting, but keeping this note here as a reminder. - var isAuthority = HasAuthority || (AllowOwnerToParent && IsOwner); + var isAuthority = InternalHasAuthority() || (AllowOwnerToParent && IsOwner); // If we don't have authority and we are not shutting down, then don't allow any parenting. // If we are shutting down and don't have authority then allow it. @@ -2412,7 +2407,7 @@ private void OnTransformParentChanged() // With distributed authority, we need to track "valid authoritative" parenting changes. // So, either the authority or AuthorityAppliedParenting is considered a "valid parenting change". - var isParentingAuthority = HasAuthority || AuthorityAppliedParenting || (AllowOwnerToParent && IsOwner); + var isParentingAuthority = InternalHasAuthority() || AuthorityAppliedParenting || (AllowOwnerToParent && IsOwner); // If we are spawned and don't have authority; reset the parent back to the cached parent and exit if (!isParentingAuthority) { @@ -3407,7 +3402,6 @@ internal static NetworkObject Deserialize(in SerializedObject serializedObject, { Destroy(networkObject.gameObject); } - return null; } @@ -3529,7 +3523,7 @@ internal void SceneChangedUpdate(Scene scene, bool notify = false) return; } - var isAuthority = HasAuthority; + var isAuthority = InternalHasAuthority(); SceneOriginHandle = scene.handle; // non-authority needs to update the NetworkSceneHandle diff --git a/com.unity.netcode.gameobjects/Runtime/SceneManagement/DefaultSceneManagerHandler.cs b/com.unity.netcode.gameobjects/Runtime/SceneManagement/DefaultSceneManagerHandler.cs index 76ea8feb04..c4041b18c1 100644 --- a/com.unity.netcode.gameobjects/Runtime/SceneManagement/DefaultSceneManagerHandler.cs +++ b/com.unity.netcode.gameobjects/Runtime/SceneManagement/DefaultSceneManagerHandler.cs @@ -302,7 +302,7 @@ public void MoveObjectsFromSceneToDontDestroyOnLoad(ref NetworkManager networkMa } // Check to determine if we need to allow destroying a non-authority instance - if (distributedAuthority && networkObject.DestroyWithScene && !networkObject.HasAuthority) + if (distributedAuthority && networkObject.DestroyWithScene && !networkObject.InternalHasAuthority()) { networkObject.DestroyPendingSceneEvent = true; } @@ -316,7 +316,7 @@ public void MoveObjectsFromSceneToDontDestroyOnLoad(ref NetworkManager networkMa UnityEngine.Object.DontDestroyOnLoad(networkObject.gameObject); } } - else if (networkObject.HasAuthority) + else if (networkObject.InternalHasAuthority()) { // We know this instance is going to be destroyed (when it receives the destroy object message). // We have to invoke this prior to invoking despawn in order to know that we are de-spawning in diff --git a/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs b/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs index 8c07fbd603..1a4fa0d146 100644 --- a/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs +++ b/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs @@ -2713,7 +2713,7 @@ internal void MoveObjectsToDontDestroyOnLoad() } // Check to determine if we need to allow destroying a non-authority instance - if (distributedAuthority && networkObject.DestroyWithScene && !networkObject.HasAuthority) + if (distributedAuthority && networkObject.DestroyWithScene && !networkObject.InternalHasAuthority()) { networkObject.DestroyPendingSceneEvent = true; } @@ -2731,7 +2731,7 @@ internal void MoveObjectsToDontDestroyOnLoad() networkObject.SceneOriginHandle = networkObject.gameObject.scene.handle; } } - else if (networkObject.HasAuthority) + else if (networkObject.InternalHasAuthority()) { networkObject.SetIsDestroying(); // Only destroy non-scene placed NetworkObjects to avoid warnings about destroying in-scene placed NetworkObjects. @@ -2889,7 +2889,7 @@ internal bool IsSceneUnloading(NetworkObject networkObject) internal void NotifyNetworkObjectSceneChanged(NetworkObject networkObject) { // Really, this should never happen but in case it does - if (!networkObject.HasAuthority) + if (!networkObject.InternalHasAuthority()) { if (NetworkManager.LogLevel == LogLevel.Developer) { diff --git a/com.unity.netcode.gameobjects/Runtime/Spawning/NetworkSpawnManager.cs b/com.unity.netcode.gameobjects/Runtime/Spawning/NetworkSpawnManager.cs index 46f52bc4f9..e868424998 100644 --- a/com.unity.netcode.gameobjects/Runtime/Spawning/NetworkSpawnManager.cs +++ b/com.unity.netcode.gameobjects/Runtime/Spawning/NetworkSpawnManager.cs @@ -1574,7 +1574,7 @@ internal void ServerDestroySpawnedSceneObjects() if (networkObject.InScenePlaced && networkObject.DestroyWithScene && networkObject.gameObject.scene != NetworkManager.SceneManager.DontDestroyOnLoadScene) { - if (networkObject.IsSpawned && networkObject.HasAuthority) + if (networkObject.IsSpawned && networkObject.InternalHasAuthority()) { networkObject.Despawn(false); } @@ -1729,7 +1729,7 @@ internal void ServerSpawnSceneObjectsOnStartSweep() /// internal void OnDespawnNonAuthorityObject([NotNull] NetworkObject networkObject, bool destroyGameObject) { - if (networkObject.HasAuthority) + if (networkObject.InternalHasAuthority()) { if (NetworkManager.LogLevel <= LogLevel.Error) { @@ -1806,7 +1806,7 @@ internal void OnDespawnObject([NotNull] NetworkObject networkObject, bool destro } // For mixed authority hierarchies, if the parent is despawned then any removal of children // is considered "authority approved". Set the AuthorityAppliedParenting flag. - spawnedNetObj.AuthorityAppliedParenting = distributedAuthority && !networkObject.HasAuthority; + spawnedNetObj.AuthorityAppliedParenting = distributedAuthority && !networkObject.InternalHasAuthority(); // Try to remove the parent using the cached WorldPositionStays value // Note: WorldPositionStays will still default to true if this was an @@ -1834,7 +1834,7 @@ internal void OnDespawnObject([NotNull] NetworkObject networkObject, bool destro networkObject.InvokeBehaviourNetworkDespawn(); // Whether we are in distributedAuthority mode and have authority on this object - var hasDAAuthority = distributedAuthority && (networkObject.HasAuthority || (NetworkManager.DAHost && authorityOverride)); + var hasDAAuthority = distributedAuthority && (networkObject.InternalHasAuthority() || (NetworkManager.DAHost && authorityOverride)); // Don't send messages if shutting down // Otherwise send messages if we are the authority (either the server, or the DA mode authority of this object). From f5ea5cbb18b07dc514d0710332c45c1a0494679f Mon Sep 17 00:00:00 2001 From: Noellie Velez Date: Thu, 11 Jun 2026 11:13:50 +0200 Subject: [PATCH 3/6] Revert IsSpawn check --- .../Runtime/Core/NetworkObject.cs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs b/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs index cb0b23006b..affe436e80 100644 --- a/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs +++ b/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs @@ -861,7 +861,7 @@ public OwnershipRequestStatus RequestOwnership() public OnOwnershipRequestedDelegateHandler OnOwnershipRequested; /// - /// Invoked by ChangeOwnershipMessage + /// Invoked by /// /// the client requesting ownership internal void OwnershipRequest(ulong clientRequestingOwnership) @@ -1155,11 +1155,8 @@ public bool HasOwnershipStatus(OwnershipStatus status) [MethodImpl(MethodImplOptions.AggressiveInlining)] internal bool InternalHasAuthority() { - if (!IsSpawned) - { - return false; - } - return NetworkManagerOwner.DistributedAuthorityMode ? OwnerClientId == NetworkManagerOwner.LocalClientId : NetworkManagerOwner.IsServer; + var networkManager = NetworkManager; + return networkManager.DistributedAuthorityMode ? OwnerClientId == networkManager.LocalClientId : networkManager.IsServer; } /// From 75b7480d0f1a0448fb3b90071b0b3e6d770b1a14 Mon Sep 17 00:00:00 2001 From: Noellie Velez Date: Thu, 11 Jun 2026 11:36:53 +0200 Subject: [PATCH 4/6] Revert private to internal change --- .../Runtime/Core/NetworkBehaviour.cs | 2 +- .../Runtime/Core/NetworkObject.cs | 2 +- .../Runtime/SceneManagement/DefaultSceneManagerHandler.cs | 4 ++-- .../Runtime/SceneManagement/NetworkSceneManager.cs | 6 +++--- .../Runtime/Spawning/NetworkSpawnManager.cs | 8 ++++---- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs b/com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs index ab92df14c3..16b6d3be62 100644 --- a/com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs +++ b/com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs @@ -670,7 +670,7 @@ internal void UpdateNetworkProperties() IsClient = m_NetworkManager.IsListening && m_NetworkManager.IsClient; IsServer = m_NetworkManager.IsListening && m_NetworkManager.IsServer; IsSessionOwner = m_NetworkManager.IsListening && m_NetworkManager.LocalClient.IsSessionOwner; - HasAuthority = m_NetworkObject.InternalHasAuthority(); + HasAuthority = m_NetworkObject.HasAuthority; ServerIsHost = m_NetworkManager.IsListening && m_NetworkManager.ServerIsHost; } } diff --git a/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs b/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs index affe436e80..744b142d96 100644 --- a/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs +++ b/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs @@ -1153,7 +1153,7 @@ public bool HasOwnershipStatus(OwnershipStatus status) public bool HasAuthority => InternalHasAuthority(); [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal bool InternalHasAuthority() + private bool InternalHasAuthority() { var networkManager = NetworkManager; return networkManager.DistributedAuthorityMode ? OwnerClientId == networkManager.LocalClientId : networkManager.IsServer; diff --git a/com.unity.netcode.gameobjects/Runtime/SceneManagement/DefaultSceneManagerHandler.cs b/com.unity.netcode.gameobjects/Runtime/SceneManagement/DefaultSceneManagerHandler.cs index c4041b18c1..76ea8feb04 100644 --- a/com.unity.netcode.gameobjects/Runtime/SceneManagement/DefaultSceneManagerHandler.cs +++ b/com.unity.netcode.gameobjects/Runtime/SceneManagement/DefaultSceneManagerHandler.cs @@ -302,7 +302,7 @@ public void MoveObjectsFromSceneToDontDestroyOnLoad(ref NetworkManager networkMa } // Check to determine if we need to allow destroying a non-authority instance - if (distributedAuthority && networkObject.DestroyWithScene && !networkObject.InternalHasAuthority()) + if (distributedAuthority && networkObject.DestroyWithScene && !networkObject.HasAuthority) { networkObject.DestroyPendingSceneEvent = true; } @@ -316,7 +316,7 @@ public void MoveObjectsFromSceneToDontDestroyOnLoad(ref NetworkManager networkMa UnityEngine.Object.DontDestroyOnLoad(networkObject.gameObject); } } - else if (networkObject.InternalHasAuthority()) + else if (networkObject.HasAuthority) { // We know this instance is going to be destroyed (when it receives the destroy object message). // We have to invoke this prior to invoking despawn in order to know that we are de-spawning in diff --git a/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs b/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs index 1a4fa0d146..8c07fbd603 100644 --- a/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs +++ b/com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs @@ -2713,7 +2713,7 @@ internal void MoveObjectsToDontDestroyOnLoad() } // Check to determine if we need to allow destroying a non-authority instance - if (distributedAuthority && networkObject.DestroyWithScene && !networkObject.InternalHasAuthority()) + if (distributedAuthority && networkObject.DestroyWithScene && !networkObject.HasAuthority) { networkObject.DestroyPendingSceneEvent = true; } @@ -2731,7 +2731,7 @@ internal void MoveObjectsToDontDestroyOnLoad() networkObject.SceneOriginHandle = networkObject.gameObject.scene.handle; } } - else if (networkObject.InternalHasAuthority()) + else if (networkObject.HasAuthority) { networkObject.SetIsDestroying(); // Only destroy non-scene placed NetworkObjects to avoid warnings about destroying in-scene placed NetworkObjects. @@ -2889,7 +2889,7 @@ internal bool IsSceneUnloading(NetworkObject networkObject) internal void NotifyNetworkObjectSceneChanged(NetworkObject networkObject) { // Really, this should never happen but in case it does - if (!networkObject.InternalHasAuthority()) + if (!networkObject.HasAuthority) { if (NetworkManager.LogLevel == LogLevel.Developer) { diff --git a/com.unity.netcode.gameobjects/Runtime/Spawning/NetworkSpawnManager.cs b/com.unity.netcode.gameobjects/Runtime/Spawning/NetworkSpawnManager.cs index e868424998..46f52bc4f9 100644 --- a/com.unity.netcode.gameobjects/Runtime/Spawning/NetworkSpawnManager.cs +++ b/com.unity.netcode.gameobjects/Runtime/Spawning/NetworkSpawnManager.cs @@ -1574,7 +1574,7 @@ internal void ServerDestroySpawnedSceneObjects() if (networkObject.InScenePlaced && networkObject.DestroyWithScene && networkObject.gameObject.scene != NetworkManager.SceneManager.DontDestroyOnLoadScene) { - if (networkObject.IsSpawned && networkObject.InternalHasAuthority()) + if (networkObject.IsSpawned && networkObject.HasAuthority) { networkObject.Despawn(false); } @@ -1729,7 +1729,7 @@ internal void ServerSpawnSceneObjectsOnStartSweep() /// internal void OnDespawnNonAuthorityObject([NotNull] NetworkObject networkObject, bool destroyGameObject) { - if (networkObject.InternalHasAuthority()) + if (networkObject.HasAuthority) { if (NetworkManager.LogLevel <= LogLevel.Error) { @@ -1806,7 +1806,7 @@ internal void OnDespawnObject([NotNull] NetworkObject networkObject, bool destro } // For mixed authority hierarchies, if the parent is despawned then any removal of children // is considered "authority approved". Set the AuthorityAppliedParenting flag. - spawnedNetObj.AuthorityAppliedParenting = distributedAuthority && !networkObject.InternalHasAuthority(); + spawnedNetObj.AuthorityAppliedParenting = distributedAuthority && !networkObject.HasAuthority; // Try to remove the parent using the cached WorldPositionStays value // Note: WorldPositionStays will still default to true if this was an @@ -1834,7 +1834,7 @@ internal void OnDespawnObject([NotNull] NetworkObject networkObject, bool destro networkObject.InvokeBehaviourNetworkDespawn(); // Whether we are in distributedAuthority mode and have authority on this object - var hasDAAuthority = distributedAuthority && (networkObject.InternalHasAuthority() || (NetworkManager.DAHost && authorityOverride)); + var hasDAAuthority = distributedAuthority && (networkObject.HasAuthority || (NetworkManager.DAHost && authorityOverride)); // Don't send messages if shutting down // Otherwise send messages if we are the authority (either the server, or the DA mode authority of this object). From 84fe50ffd9476d11f38b77b7f6659f63d590e54f Mon Sep 17 00:00:00 2001 From: Noellie Velez Date: Mon, 15 Jun 2026 13:27:07 +0200 Subject: [PATCH 5/6] Revert cleanup - to move to other branch --- .../Runtime/Core/NetworkObject.cs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs b/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs index 744b142d96..f73b187945 100644 --- a/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs +++ b/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs @@ -395,13 +395,14 @@ private void CheckForInScenePlaced() /// Defaults to true, determines whether the will be destroyed. public void DeferDespawn(int tickOffset, bool destroy = true) { - // The DAMode message is logged first, as ownership locking isn’t allowed when not in DAMode, making it the most relevant message. + // Ensure we log the DAMode message first as locking ownership is not allowed if not DA so the DA message is the most relevant. if (!NetworkManager.DistributedAuthorityMode) { if (NetworkManager.LogLevel <= LogLevel.Error) { NetworkLog.LogErrorServer($"[{name}] This method is only available in distributed authority mode."); } + return; } @@ -411,6 +412,7 @@ public void DeferDespawn(int tickOffset, bool destroy = true) { NetworkLog.LogErrorServer($"[{name}] Cannot defer despawn while not spawned."); } + return; } @@ -610,7 +612,7 @@ internal void RemoveOwnershipExtended(OwnershipStatusExtended extended) /// true or false depending upon lock operation's success public bool SetOwnershipLock(bool lockOwnership = true) { - // The DAMode message is logged first, as ownership locking isn’t allowed when not in DAMode, making it the most relevant message. + // Ensure we log the DAMode message first as locking ownership is not allowed if not DA so the DA message is the most relevant. if (!NetworkManager.DistributedAuthorityMode) { if (NetworkManager.LogLevel <= LogLevel.Error) @@ -626,6 +628,7 @@ public bool SetOwnershipLock(bool lockOwnership = true) { NetworkLog.LogErrorServer($"[{name}][Attempted Lock While not spawned]"); } + return false; } @@ -668,6 +671,7 @@ public bool SetOwnershipLock(bool lockOwnership = true) { SendOwnershipStatusUpdate(); } + return true; } @@ -793,6 +797,7 @@ public OwnershipRequestStatus RequestOwnership() { NetworkLog.LogErrorServer($"[{name}][Invalid Operation] Cannot request ownership of an NetworkObject before it is spawned."); } + return OwnershipRequestStatus.InvalidOperation; } // Exit early the local client is already the owner @@ -861,7 +866,7 @@ public OwnershipRequestStatus RequestOwnership() public OnOwnershipRequestedDelegateHandler OnOwnershipRequested; /// - /// Invoked by + /// Invoked by ChangeOwnershipMessage /// /// the client requesting ownership internal void OwnershipRequest(ulong clientRequestingOwnership) @@ -3399,6 +3404,7 @@ internal static NetworkObject Deserialize(in SerializedObject serializedObject, { Destroy(networkObject.gameObject); } + return null; } @@ -3555,7 +3561,7 @@ internal void SceneChangedUpdate(Scene scene, bool notify = false) OnMigratedToNewScene?.Invoke(); // Only the authority side will notify clients of non-parented NetworkObject scene changes - if (isAuthority && notify && transform.parent == null) + if (isAuthority && notify && !transform.parent) { NetworkManagerOwner.SceneManager.NotifyNetworkObjectSceneChanged(this); } From 51fa3661d3b8090067aabd840467e10da22a57d2 Mon Sep 17 00:00:00 2001 From: Noellie Velez Date: Mon, 15 Jun 2026 17:50:09 +0200 Subject: [PATCH 6/6] Convert HasAuthority method into a field Update it on Spawn, InvokeBehaviourOnOwnershipChanged and ResetOnDespawn --- .../Runtime/Core/NetworkObject.cs | 41 +++++++++---------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs b/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs index f73b187945..3975888ab4 100644 --- a/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs +++ b/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs @@ -416,7 +416,7 @@ public void DeferDespawn(int tickOffset, bool destroy = true) return; } - if (!InternalHasAuthority()) + if (!m_HasAuthority) { if (NetworkManagerOwner.LogLevel <= LogLevel.Error) { @@ -633,7 +633,7 @@ public bool SetOwnershipLock(bool lockOwnership = true) } // If we don't have authority exit early - if (!InternalHasAuthority()) + if (!m_HasAuthority) { if (NetworkManager.LogLevel <= LogLevel.Error) { @@ -909,7 +909,7 @@ internal void OwnershipRequest(ulong clientRequestingOwnership) // This action is always authorized as long as the client still has authority. // We need to pass in that this is a request approval ownership change. - NetworkManagerOwner.SpawnManager.ChangeOwnership(this, clientRequestingOwnership, InternalHasAuthority(), true); + NetworkManagerOwner.SpawnManager.ChangeOwnership(this, clientRequestingOwnership, m_HasAuthority, true); } else { @@ -1155,14 +1155,9 @@ public bool HasOwnershipStatus(OwnershipStatus status) /// /// When in client-server mode, authority should is not considered the same as ownership. /// - public bool HasAuthority => InternalHasAuthority(); + public bool HasAuthority => IsSpawned ? m_HasAuthority : NetworkManager.IsServer; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private bool InternalHasAuthority() - { - var networkManager = NetworkManager; - return networkManager.DistributedAuthorityMode ? OwnerClientId == networkManager.LocalClientId : networkManager.IsServer; - } + private bool m_HasAuthority; /// /// The NetworkManager that owns this NetworkObject. @@ -1506,7 +1501,7 @@ public void NetworkShow(ulong clientId) return; } - if (!InternalHasAuthority()) + if (!m_HasAuthority) { if (NetworkManagerOwner.DistributedAuthorityMode) { @@ -1601,7 +1596,7 @@ public void NetworkHide(ulong clientId) return; } - if (!InternalHasAuthority()) + if (!m_HasAuthority) { if (NetworkManagerOwner.DistributedAuthorityMode) { @@ -1760,7 +1755,7 @@ private void OnDestroy() { // An authorized destroy is when done by the authority instance or done due to a scene event and the NetworkObject // was marked as destroy pending scene event (which means the destroy with scene property was set). - var isAuthorityDestroy = InternalHasAuthority() || NetworkManager.DAHost || DestroyPendingSceneEvent; + var isAuthorityDestroy = m_HasAuthority || NetworkManager.DAHost || DestroyPendingSceneEvent; // If the NetworkObject's GameObject is still valid and the scene is still valid and loaded, then we are still valid var isStillValid = gameObject != null && gameObject.scene.IsValid() && gameObject.scene.isLoaded; @@ -2005,8 +2000,8 @@ public NetworkObject InstantiateAndSpawn(NetworkManager networkManager, ulong ow /// Should the object be destroyed when the scene is changed public void Spawn(bool destroyWithScene = false) { - var networkManager = NetworkManager; - var clientId = networkManager.DistributedAuthorityMode ? networkManager.LocalClientId : NetworkManager.ServerClientId; + var clientId = NetworkManager.DistributedAuthorityMode ? NetworkManager.LocalClientId : NetworkManager.ServerClientId; + m_HasAuthority = NetworkManager.DistributedAuthorityMode ? OwnerClientId == NetworkManager.LocalClientId : NetworkManager.IsServer; SpawnInternal(destroyWithScene, clientId, false); } @@ -2062,6 +2057,7 @@ internal void ResetOnDespawn() { // Always clear out the observers list when despawned Observers.Clear(); + m_HasAuthority = false; IsSpawnAuthority = false; IsSpawned = false; DeferredDespawnTick = 0; @@ -2099,7 +2095,7 @@ public void ChangeOwnership(ulong newOwnerClientId) } return; } - NetworkManagerOwner.SpawnManager.ChangeOwnership(this, newOwnerClientId, InternalHasAuthority()); + NetworkManagerOwner.SpawnManager.ChangeOwnership(this, newOwnerClientId, m_HasAuthority); } /// @@ -2122,6 +2118,8 @@ internal void InvokeBehaviourOnOwnershipChanged(ulong originalOwnerClientId, ulo var isPreviousOwner = originalOwnerClientId == NetworkManagerOwner.LocalClientId; var isNewOwner = newOwnerClientId == NetworkManagerOwner.LocalClientId; + m_HasAuthority = distributedAuthorityMode ? OwnerClientId == NetworkManagerOwner.LocalClientId : NetworkManagerOwner.IsServer; + if (distributedAuthorityMode || isPreviousOwner) { NetworkManagerOwner.SpawnManager.UpdateOwnershipTable(this, originalOwnerClientId, true); @@ -2334,7 +2332,7 @@ public bool TrySetParent(NetworkObject parent, bool worldPositionStays = true) // DANGO-TODO: Do we want to worry about ownership permissions here? // It wouldn't make sense to not allow parenting, but keeping this note here as a reminder. - var isAuthority = InternalHasAuthority() || (AllowOwnerToParent && IsOwner); + var isAuthority = m_HasAuthority || (AllowOwnerToParent && IsOwner); // If we don't have authority and we are not shutting down, then don't allow any parenting. // If we are shutting down and don't have authority then allow it. @@ -2409,7 +2407,7 @@ private void OnTransformParentChanged() // With distributed authority, we need to track "valid authoritative" parenting changes. // So, either the authority or AuthorityAppliedParenting is considered a "valid parenting change". - var isParentingAuthority = InternalHasAuthority() || AuthorityAppliedParenting || (AllowOwnerToParent && IsOwner); + var isParentingAuthority = m_HasAuthority || AuthorityAppliedParenting || (AllowOwnerToParent && IsOwner); // If we are spawned and don't have authority; reset the parent back to the cached parent and exit if (!isParentingAuthority) { @@ -3526,15 +3524,14 @@ internal void SceneChangedUpdate(Scene scene, bool notify = false) return; } - var isAuthority = InternalHasAuthority(); SceneOriginHandle = scene.handle; // non-authority needs to update the NetworkSceneHandle - if (!isAuthority && NetworkManagerOwner.SceneManager.ClientSceneHandleToServerSceneHandle.ContainsKey(SceneOriginHandle)) + if (!m_HasAuthority && NetworkManagerOwner.SceneManager.ClientSceneHandleToServerSceneHandle.ContainsKey(SceneOriginHandle)) { NetworkSceneHandle = NetworkManagerOwner.SceneManager.ClientSceneHandleToServerSceneHandle[SceneOriginHandle]; } - else if (isAuthority) + else if (m_HasAuthority) { // Since the authority is the source of truth for the NetworkSceneHandle, // the NetworkSceneHandle is the same as the SceneOriginHandle. @@ -3561,7 +3558,7 @@ internal void SceneChangedUpdate(Scene scene, bool notify = false) OnMigratedToNewScene?.Invoke(); // Only the authority side will notify clients of non-parented NetworkObject scene changes - if (isAuthority && notify && !transform.parent) + if (m_HasAuthority && notify && !transform.parent) { NetworkManagerOwner.SceneManager.NotifyNetworkObjectSceneChanged(this); }