menu_bookDocumentation
Network Ownership System
Network Ownership System
Control who simulates and updates networked GameObjects with the ownership system.
Ownership Basics
Every networked object has an Owner — the client or host responsible for:
- Sending position/rotation/scale updates
- Modifying Sync properties
- Performing certain RPC calls
Default Ownership
| Scenario | Default Owner |
|---|---|
| Scene-created networked objects | None (host simulates) |
| Runtime NetworkSpawn() | Spawning client |
| Player prefab spawn | Player's connection |
Checking Ownership
CSHARP
// Check if this client is the owner
bool isOwner = Network.IsOwner;
// Check the owner ID
long ownerId = Network.OwnerId;
// Check if simulated by someone else (proxy)
bool isProxy = IsProxy;
// Typical pattern - only owner runs logic
if (IsProxy) return;Taking Ownership
CSHARP
// Take ownership of an object
GameObject.Network.TakeOwnership();
// Check if ownership was successful
if (Network.IsOwner)
{
// Now controls this object
}Dropping Ownership
CSHARP
// Release ownership back to host/none
GameObject.Network.DropOwnership();Owner Transfer Modes
Control who can change ownership:
CSHARP
// Anyone can take ownership
GameObject.Network.SetOwnerTransfer(OwnerTransfer.Takeover);
// Only host can change (default)
GameObject.Network.SetOwnerTransfer(OwnerTransfer.Fixed);
// Request-based (owner can approve/deny)
GameObject.Network.SetOwnerTransfer(OwnerTransfer.Request);Ownership Transfer Events
CSHARP
public class OwnableItem : Component
{
protected override void OnAwake()
{
Network.OnOwnerChanged += OnOwnerChanged;
}
private void OnOwnerChanged(long newOwner, long oldOwner)
{
Log.Info($"Ownership changed from {oldOwner} to {newOwner}");
// Update visuals based on ownership
if (Network.IsOwner)
{
ShowControls();
}
else
{
HideControls();
}
}
}Ownership with Sync Properties
Sync properties are controlled by the owner:
CSHARP
public class PlayerState : Component
{
// Only owner can modify, replicated to all
[Sync] public int Health { get; set; }
// Owner controls, others read-only
protected override void OnUpdate()
{
if (!Network.IsOwner) return;
// Only owner calculates damage
if (takingDamage) Health -= damage;
}
}Disconnection Handling
When an owner disconnects:
- Objects they own are destroyed by default
- Override OnDisconnected in INetworkListener to customize behavior
- Consider transferring ownership before destruction
CSHARP
public void OnDisconnected(Connection connection)
{
// Find objects owned by disconnecting player
foreach (var obj in Scene.GetAllObjects(true))
{
if (obj.Network.OwnerId == connection.Id)
{
// Transfer to host or destroy
obj.Network.DropOwnership();
}
}
}
Was this helpful?