Remote procedure calls let different computers or devices talk and work together by asking each other to run specific tasks.
Remote procedure calls in Unity
using UnityEngine;
using Unity.Netcode;
public class ExampleRPC : NetworkBehaviour
{
[ServerRpc]
public void MyServerRpc()
{
// Code runs on the server
}
[ClientRpc]
public void MyClientRpc()
{
// Code runs on clients
}
}[ServerRpc] marks a method that clients call to run code on the server.
[ClientRpc] marks a method that the server calls to run code on all clients.
using UnityEngine; using Unity.Netcode; public class PlayerActions : NetworkBehaviour { [ServerRpc] public void JumpServerRpc() { Debug.Log("Player jumped on server"); } }
using UnityEngine; using Unity.Netcode; public class GameEvents : NetworkBehaviour { [ClientRpc] public void PlaySoundClientRpc() { Debug.Log("Play sound on all clients"); } }
This program lets the player press space to jump. The jump is sent to the server with a ServerRpc. The server logs the jump and tells all clients to play a jump sound with a ClientRpc.
using UnityEngine; using Unity.Netcode; public class RPCExample : NetworkBehaviour { void Update() { if (IsOwner && Input.GetKeyDown(KeyCode.Space)) { JumpServerRpc(); } } [ServerRpc] void JumpServerRpc() { Debug.Log("Server: Player jumped"); PlayJumpSoundClientRpc(); } [ClientRpc] void PlayJumpSoundClientRpc() { Debug.Log("Client: Play jump sound"); } }
Only the player who owns the object should call ServerRpc to avoid errors.
ServerRpc methods run on the server, ClientRpc methods run on all clients.
RPCs help keep multiplayer games in sync by sharing actions across devices.
Remote procedure calls let devices ask each other to run code.
Use [ServerRpc] for client-to-server calls.
Use [ClientRpc] for server-to-client calls.
Practice
Solution
Step 1: Understand the direction of the call
Client-to-server calls require a special attribute to mark the method that runs on the server.Step 2: Identify the correct attribute in Unity
Unity uses[ServerRpc]to mark methods that clients call to run on the server.Final Answer:
[ServerRpc] -> Option AQuick Check:
Client-to-server calls use [ServerRpc] [OK]
- Confusing [ClientRpc] as client-to-server
- Using non-existent attributes like [RunOnServer]
- Thinking [RemoteCall] is a valid Unity attribute
Solution
Step 1: Recall the attribute placement
Attributes in C# go before the method declaration.Step 2: Check the correct attribute and syntax
The correct syntax is[ServerRpc] public void MyMethod() { }. void [ServerRpc] MyMethod() { } places the attribute incorrectly.Final Answer:
[ServerRpc] public void MyMethod() { } -> Option BQuick Check:
Attributes precede method declarations [OK]
- Placing attribute after method name
- Using [ClientRpc] for server RPC
- Omitting the attribute entirely
[ServerRpc]
public void SendMessageToClients(string msg) {
Debug.Log("Server received: " + msg);
BroadcastMessageClientRpc(msg);
}
[ClientRpc]
void BroadcastMessageClientRpc(string message) {
Debug.Log("Client received: " + message);
}
What will be printed on the client when SendMessageToClients("Hello") is called from a client?Solution
Step 1: Understand the call flow
The client callsSendMessageToClientswhich runs on the server due to [ServerRpc]. The server logs "Server received: Hello".Step 2: Server calls client method
The server then callsBroadcastMessageClientRpcon all clients, which logs "Client received: Hello" on each client.Final Answer:
Client received: Hello -> Option CQuick Check:
ClientRpc runs on clients, so clients see "Client received: Hello" [OK]
- Thinking server logs appear on client
- Assuming both logs print on client
- Confusing which method runs where
[ServerRpc]
public void UpdateScoreClientRpc(int score) {
Debug.Log("Score updated: " + score);
}Solution
Step 1: Analyze method name and attribute
The method name contains "ClientRpc" but the attribute is [ServerRpc], which is confusing and likely a mistake.Step 2: Check RPC method rules
RPC methods can have parameters and must have a return type (void is valid). They can be public. The mismatch between name and attribute is the main issue.Final Answer:
Method name suggests client RPC but uses [ServerRpc] attribute -> Option DQuick Check:
Method name and attribute must match RPC direction [OK]
- Ignoring method name and attribute mismatch
- Thinking parameters are not allowed
- Assuming RPC methods must be private
Solution
Step 1: Understand RPC direction
Server sends messages to clients using [ClientRpc]. To exclude a client, the server must know which client to exclude.Step 2: Use NetworkConnection parameter
Unity allows [ClientRpc] methods to take aNetworkConnectionparameter to exclude that client from receiving the RPC.Step 3: Confirm other options
Use [ClientRpc] with a parameter to exclude the sender client inside the method is incorrect because exclusion logic cannot be done inside the client method. Use [ServerRpc] to send message to clients except sender is wrong direction. Use [ClientRpc] and check inside the client to ignore messages from itself relies on client ignoring messages, which is inefficient.Final Answer:
Use [ClientRpc] with a special NetworkConnection parameter to exclude sender -> Option AQuick Check:
Exclude sender with NetworkConnection parameter in ClientRpc [OK]
- Trying to exclude sender inside client code
- Using ServerRpc to send to clients
- Not using NetworkConnection parameter for exclusion
Ready to prove it?
Pass the test and this pattern moves unknown
