Bird
Raised Fist0
Unityframework~20 mins

Remote procedure calls in Unity - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
RPC Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Unity RPC call?

Consider the following Unity C# code snippet using [ClientRpc]. What will be printed in the console when the server calls RpcShowMessage()?

Unity
using Unity.Netcode;
using UnityEngine;

public class MessageHandler : NetworkBehaviour
{
    [ClientRpc]
    public void RpcShowMessage()
    {
        Debug.Log("Hello from server to clients!");
    }

    public override void OnNetworkSpawn()
    {
        if (IsServer)
        {
            RpcShowMessage();
        }
    }
}
AAll clients and the server print: Hello from server to clients!
BOnly the server console prints: Hello from server to clients!
CNo output is printed because RpcShowMessage is not called on clients.
DOnly clients print: Hello from server to clients!, server does not print.
Attempts:
2 left
💡 Hint

Remember that [ClientRpc] methods run on clients when called by the server.

❓ Predict Output
intermediate
2:00remaining
What happens when a client calls a ServerRpc without permission?

Given this Unity C# code snippet, what will happen if a client calls CmdChangeScore(10)?

Unity
using Unity.Netcode;
using UnityEngine;

public class ScoreManager : NetworkBehaviour
{
    private int score = 0;

    [ServerRpc]
    public void CmdChangeScore(int amount)
    {
        score += amount;
        Debug.Log($"Score changed to {score}");
    }
}
AThe method runs on the client instead of the server, so score is not updated.
BThe server updates the score and prints the new value.
CThe server throws a security exception and disconnects the client.
DNothing happens because clients cannot call ServerRpc methods.
Attempts:
2 left
💡 Hint

Clients can call [ServerRpc] methods to request actions on the server.

🔧 Debug
advanced
2:00remaining
Why does this ServerRpc not execute on the server?

Examine the following code. The CmdSendData method is never executed on the server when called by a client. What is the cause?

Unity
using Unity.Netcode;
using UnityEngine;

public class DataSender : NetworkBehaviour
{
    [ServerRpc]
    public void CmdSendData(string data)
    {
        Debug.Log($"Data received: {data}");
    }

    void Update()
    {
        if (IsClient && Input.GetKeyDown(KeyCode.Space))
        {
            CmdSendData("Hello Server");
        }
    }
}
AThe method name must start with 'Rpc' to be recognized as a ServerRpc.
BThe client is not connected to the server, so the ServerRpc never reaches the server.
CThe method is missing the <code>RequireOwnership = false</code> attribute, so the client must own the object to call the ServerRpc.
DThe method is private and cannot be called from clients.
Attempts:
2 left
💡 Hint

Check ownership requirements for ServerRpc calls.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in this ClientRpc method

Which option correctly fixes the syntax error in this ClientRpc method?

Unity
using Unity.Netcode;
using UnityEngine;

public class Notifier : NetworkBehaviour
{
    [ClientRpc]
    public void RpcNotify(string message)
    {
        Debug.Log(message)
    }
}
AChange the method to static void RpcNotify(string message)
BChange Debug.Log(message) to Debug.Log(message); return;
CRemove the parentheses from Debug.Log to Debug.Log;
DAdd a semicolon after Debug.Log(message);
Attempts:
2 left
💡 Hint

Check for missing punctuation in C# statements.

🚀 Application
expert
3:00remaining
How to synchronize a player's health using RPCs?

You want to keep all clients updated with the player's health value in a multiplayer Unity game using Netcode. Which approach using RPCs is best?

AUse a <code>[ServerRpc]</code> method for clients to send health changes to the server, then a <code>[ClientRpc]</code> method to broadcast the updated health to all clients.
BUse only a <code>[ClientRpc]</code> method to send health updates from clients to the server.
CUse only a <code>[ServerRpc]</code> method to send health updates from the server to clients.
DUse local variables on each client and do not use RPCs for health synchronization.
Attempts:
2 left
💡 Hint

Think about how data flows from clients to server and then to all clients.

Practice

(1/5)
1. What attribute do you use in Unity to make a method run on the server when called from a client?
easy
A. [ServerRpc]
B. [ClientRpc]
C. [RunOnServer]
D. [RemoteCall]

Solution

  1. 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.
  2. Step 2: Identify the correct attribute in Unity

    Unity uses [ServerRpc] to mark methods that clients call to run on the server.
  3. Final Answer:

    [ServerRpc] -> Option A
  4. Quick Check:

    Client-to-server calls use [ServerRpc] [OK]
Hint: Client calls server methods with [ServerRpc] attribute [OK]
Common Mistakes:
  • Confusing [ClientRpc] as client-to-server
  • Using non-existent attributes like [RunOnServer]
  • Thinking [RemoteCall] is a valid Unity attribute
2. Which of the following is the correct syntax to declare a server RPC method in Unity?
easy
A. public void MyMethod() { }
B. [ServerRpc] public void MyMethod() { }
C. [ClientRpc] public void MyMethod() { }
D. void [ServerRpc] MyMethod() { }

Solution

  1. Step 1: Recall the attribute placement

    Attributes in C# go before the method declaration.
  2. Step 2: Check the correct attribute and syntax

    The correct syntax is [ServerRpc] public void MyMethod() { }. void [ServerRpc] MyMethod() { } places the attribute incorrectly.
  3. Final Answer:

    [ServerRpc] public void MyMethod() { } -> Option B
  4. Quick Check:

    Attributes precede method declarations [OK]
Hint: Attributes go before method signature in C# [OK]
Common Mistakes:
  • Placing attribute after method name
  • Using [ClientRpc] for server RPC
  • Omitting the attribute entirely
3. Consider this Unity code snippet:
[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?
medium
A. Server received: Hello Client received: Hello
B. Server received: Hello
C. Client received: Hello
D. Nothing is printed on client

Solution

  1. Step 1: Understand the call flow

    The client calls SendMessageToClients which runs on the server due to [ServerRpc]. The server logs "Server received: Hello".
  2. Step 2: Server calls client method

    The server then calls BroadcastMessageClientRpc on all clients, which logs "Client received: Hello" on each client.
  3. Final Answer:

    Client received: Hello -> Option C
  4. Quick Check:

    ClientRpc runs on clients, so clients see "Client received: Hello" [OK]
Hint: Client sees ClientRpc logs, server sees ServerRpc logs [OK]
Common Mistakes:
  • Thinking server logs appear on client
  • Assuming both logs print on client
  • Confusing which method runs where
4. What is wrong with this Unity RPC method?
[ServerRpc]
public void UpdateScoreClientRpc(int score) {
    Debug.Log("Score updated: " + score);
}
medium
A. [ServerRpc] methods must be private
B. Missing return type for RPC method
C. RPC methods cannot have parameters
D. Method name suggests client RPC but uses [ServerRpc] attribute

Solution

  1. Step 1: Analyze method name and attribute

    The method name contains "ClientRpc" but the attribute is [ServerRpc], which is confusing and likely a mistake.
  2. 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.
  3. Final Answer:

    Method name suggests client RPC but uses [ServerRpc] attribute -> Option D
  4. Quick Check:

    Method name and attribute must match RPC direction [OK]
Hint: Match method name with RPC attribute to avoid confusion [OK]
Common Mistakes:
  • Ignoring method name and attribute mismatch
  • Thinking parameters are not allowed
  • Assuming RPC methods must be private
5. You want to send a message from the server to all clients except the one who sent the original request. Which approach using Unity RPCs is correct?
hard
A. Use [ClientRpc] with a special NetworkConnection parameter to exclude sender
B. Use [ServerRpc] to send message to clients except sender
C. Use [ClientRpc] and check inside the client to ignore messages from itself
D. Use [ClientRpc] with a parameter to exclude the sender client inside the method

Solution

  1. Step 1: Understand RPC direction

    Server sends messages to clients using [ClientRpc]. To exclude a client, the server must know which client to exclude.
  2. Step 2: Use NetworkConnection parameter

    Unity allows [ClientRpc] methods to take a NetworkConnection parameter to exclude that client from receiving the RPC.
  3. 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.
  4. Final Answer:

    Use [ClientRpc] with a special NetworkConnection parameter to exclude sender -> Option A
  5. Quick Check:

    Exclude sender with NetworkConnection parameter in ClientRpc [OK]
Hint: Exclude sender using NetworkConnection param in ClientRpc [OK]
Common Mistakes:
  • Trying to exclude sender inside client code
  • Using ServerRpc to send to clients
  • Not using NetworkConnection parameter for exclusion
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard