0
0
Unityframework~20 mins

Remote procedure calls in Unity - Practice Problems & Coding Challenges

Choose your learning style9 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.