|
| 1 | +# NetworkBehaviour ownership |
| 2 | + |
| 3 | +Before reading these docs, ensure you understand the concepts of [ownership](../terms-concepts/ownership.md) and [NetworkObject ownership](./networkobject-ownership.md). It's also important to be familiar with the [NetworkBehaviour](./networkbehaviour.md) |
| 4 | + |
| 5 | +The owner of each NetworkBehaviour in your game is decided by the owner of that NetworkBehaviour's NetworkObject. The NetworkObject is found as a property on the NetworkBehaviour: [`NetworkBehaviour.NetworkObject`](https://docs.unity3d.com/Packages/com.unity.netcode.gameobjects@latest?subfolder=/api/Unity.Netcode.NetworkBehaviour.html#Unity_Netcode_NetworkBehaviour_NetworkObject). |
| 6 | + |
| 7 | +## Helpful properties |
| 8 | + |
| 9 | +> [!NOTE] |
| 10 | +> The following properties are only valid if the NetworkBehaviour has been spawned. Use [`NetworkBehaviour.IsSpawned`](https://docs.unity3d.com/Packages/com.unity.netcode.gameobjects@latest?subfolder=/api/Unity.Netcode.NetworkBehaviour.html#Unity_Netcode_NetworkBehaviour_IsSpawned) to check the spawned status of the NetworkBehaviour |
| 11 | +
|
| 12 | +To see if the local client is the owner of a NetworkBehaviour, you can check the[`NetworkBehaviour.IsOwner`](https://docs.unity3d.com/Packages/com.unity.netcode.gameobjects@latest?subfolder=/api/Unity.Netcode.NetworkBehaviour.html#Unity_Netcode_NetworkBehaviour_IsOwner) property. |
| 13 | + |
| 14 | +To see if the server owns a NetworkBehaviour, you can check the [`NetworkBehaviour.IsOwnedByServer`](https://docs.unity3d.com/Packages/com.unity.netcode.gameobjects@latest?subfolder=/api/Unity.Netcode.NetworkBehaviour.html#Unity_Netcode_NetworkBehaviour_IsOwnedByServer) property. |
| 15 | + |
| 16 | +To see if the local client has authority of a NetworkBehaviour, you can check the[`NetworkBehaviour.HasAuthority`](https://docs.unity3d.com/Packages/com.unity.netcode.gameobjects@latest?subfolder=/api/Unity.Netcode.NetworkBehaviour.html#Unity_Netcode_NetworkBehaviour_HasAuthority) property. |
| 17 | + |
| 18 | +## Detecting ownership changes |
| 19 | + |
| 20 | +There are three functions that can be implemented to detect ownership changes on a NetworkBehaviour. These functions are invoked in the order they are listed here. |
| 21 | + |
| 22 | +### [OnLostOwnership](https://docs.unity3d.com/Packages/com.unity.netcode.gameobjects@latest?subfolder=/api/Unity.Netcode.NetworkBehaviour.html#Unity_Netcode_NetworkBehaviour_OnLostOwnership) |
| 23 | + |
| 24 | +When using a [client-server network topology](../../terms-concepts/client-server.md) `OnLostOwnership` is invoked on both the server any time a connected client loses ownership of this NetworkBehaviour. It is also invoked on the game client who just lost ownership. |
| 25 | + |
| 26 | +In a [distributed authority network topology](../../terms-concepts/distributed-authority.md) `OnLostOwnership` is invoked on all connected game clients. |
| 27 | + |
| 28 | +```csharp |
| 29 | +void OnLostOwnership() |
| 30 | +{ |
| 31 | + var newOwner = OwnerClientId; |
| 32 | + |
| 33 | + // Take action on lost ownership here |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +### [OnGainedOwnership](https://docs.unity3d.com/Packages/com.unity.netcode.gameobjects@latest?subfolder=/api/Unity.Netcode.NetworkBehaviour.html#Unity_Netcode_NetworkBehaviour_OnGainedOwnership) |
| 38 | + |
| 39 | +When using a client-server network topology `OnGainedOwnership` is invoked on the server any time ownership is gained. It is also be invoked on the game client who just gained ownership. |
| 40 | + |
| 41 | +In a distributed authority network topology `OnGainedOwnership` is invoked on all connected game clients. |
| 42 | + |
| 43 | +`OnGainedOwnership` is invoked after `OnLostOwnership`. |
| 44 | + |
| 45 | +```csharp |
| 46 | +void OnGainedOwnership() |
| 47 | +{ |
| 48 | + var newOwner = OwnerClientId; |
| 49 | + var newOwnerIsMe = IsOwner; |
| 50 | + |
| 51 | + // Take action on ownership gain here |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +### [OnOwnershipChanged](https://docs.unity3d.com/Packages/com.unity.netcode.gameobjects@latest?subfolder=/api/Unity.Netcode.NetworkBehaviour.html#Unity_Netcode_NetworkBehaviour_OnOwnershipChanged_System_UInt64_System_UInt64_) |
| 56 | + |
| 57 | +Whenever you want notification on any and all ownership changes, implement the `OnOwnershipChanged` method. `OnOwnershipChanged` is invoked on all connected game clients whenever the ownership of the NetworkBehaviour it is implemented on changes. |
| 58 | + |
| 59 | +`OnOwnershipChanged` is invoked after `OnLostOwnership` and `OnGainedOwnership`. |
| 60 | + |
| 61 | +```csharp |
| 62 | +void OnOwnershipChanged(ulong previousOwnerId, ulong currentOwnerId) |
| 63 | +{ |
| 64 | + var newOwnerIsMe = IsOwner; |
| 65 | + |
| 66 | + // Take action on ownership change here |
| 67 | +} |
| 68 | +``` |
0 commit comments