Basic example
Without an RPC, a sound played when a button is pressed is only heard locally:[Rpc.Broadcast] to make everyone hear it:
RPC types
[Rpc.Broadcast]
[Rpc.Broadcast]
Calls the function on every connected client, including the caller. Use this for effects and events that all players should experience.
[Rpc.Owner]
[Rpc.Owner]
Calls the function only on the owner of the networked object. If the object has no owner, it falls back to the host. Use this to send instructions to the player who owns a specific object.
[Rpc.Host]
[Rpc.Host]
Calls the function only on the host. Use this when a client needs to ask the host to perform an authoritative action.
Static RPCs
Static methods can also be RPCs. A static RPC does not need to exist on aComponent:
Passing arguments
Pass arguments to an RPC like any normal method call:[Sync] properties: unmanaged value types, string, GameObject, Component, and GameResource.
Flags
Customize RPC delivery behaviour with flags:| Flag | Description |
|---|---|
NetFlags.Unreliable | Sent unreliably — may not arrive and may arrive out of order. Fast and cheap. Suitable for position updates and visual effects. |
NetFlags.Reliable | (Default) Sent reliably — retried until received. Slower and more expensive. Use for chat messages and important events. |
NetFlags.SendImmediate | Not grouped with other messages; sent immediately. Useful for real-time streaming such as voice data. |
NetFlags.DiscardOnDelay | Drop the message if it cannot be sent quickly. Only applies to unreliable messages. |
NetFlag.HostOnly | Only the host can call this RPC. |
NetFlag.OwnerOnly | Only the owner of the object can call this RPC. |
Filtering recipients
Restrict which clients receive a[Rpc.Broadcast] call using Rpc.FilterExclude or Rpc.FilterInclude:
Caller information
Check which connection triggered the RPC usingRpc.Caller:
Network events
React to players joining and leaving by implementingComponent.INetworkListener on a component placed in the scene.
INetworkListener methods
| Method | Description |
|---|---|
OnConnected | The client has connected. They are about to start handshaking, load the game, and download required packages. |
OnDisconnected | The client has disconnected. |
OnActive | The client has completed the handshake and is fully in the game. The loading screen closes after this call. |
INetworkSpawn
ImplementComponent.INetworkSpawn to react when a networked object spawns:
| Method | Description |
|---|---|
OnNetworkSpawn | Called when this object is spawned on the network. |