📖 Scenario: You are creating a simple multiplayer game in Unity where players can send messages to each other. To do this, you will use Remote Procedure Calls (RPCs) to communicate between players over the network.
🎯 Goal: Build a Unity script that uses a Remote Procedure Call to send a greeting message from one player to all other players in the game.
📋 What You'll Learn
Create a Unity C# script with a method to send a message using RPC
Set up a method that can be called remotely to display the message
Use Unity's networking system to call the RPC method
Print the received message to the Unity Console
💡 Why This Matters
🌍 Real World
Multiplayer games and networked applications use RPCs to communicate actions and data between different players or devices.
💼 Career
Understanding RPCs is important for game developers working on multiplayer games or real-time networked applications.
Progress0 / 4 steps
1
Create the basic Unity script with a message string
Create a C# script called MessageSender and inside it, declare a public string variable called message with the value "Hello from player!".
Unity
Hint
Declare a public string variable inside the class and set it to "Hello from player!".
2
Add a method to send the message using RPC
Add a public method called SendMessageToAll that uses GetComponent<PhotonView>().RPC to call a remote method named ReceiveMessage on all players. Pass the message variable as a parameter.
Unity
Hint
Use GetComponent<PhotonView>().RPC to call "ReceiveMessage" on all players with the message.
3
Create the remote method to receive and print the message
Add a private method called ReceiveMessage with a string parameter named receivedMessage. Mark it with [PunRPC]. Inside, use Debug.Log to print "Message received: " followed by receivedMessage.
Unity
Hint
Use the [PunRPC] attribute and print the received message with Debug.Log.
4
Call the send method and display the message in Console
In the Start method, call SendMessageToAll() to send the message when the game starts.
Unity
Hint
Use the Start method to call SendMessageToAll() when the game begins.
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
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 A
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
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 B
Quick Check:
Attributes precede method declarations [OK]
Hint: Attributes go before method signature in C# [OK]
D. Method name suggests client RPC but uses [ServerRpc] attribute
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 D
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
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 a NetworkConnection parameter 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 A
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 unknownstrongwhile earning +1 Grip toward your certificate.