Bird
Raised Fist0
Unityframework~20 mins

Client-server architecture in Unity - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Client-Server 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 client-server message exchange?

Consider a simple Unity client-server setup where the server sends a message to the client, and the client logs it. What will be printed in the Unity console?

Unity
using UnityEngine;

public class Server : MonoBehaviour {
    public delegate void MessageHandler(string message);
    public static event MessageHandler OnMessageSent;

    void Start() {
        SendMessageToClient("Hello from Server");
    }

    void SendMessageToClient(string msg) {
        OnMessageSent?.Invoke(msg);
    }
}

public class Client : MonoBehaviour {
    void OnEnable() {
        Server.OnMessageSent += ReceiveMessage;
    }

    void OnDisable() {
        Server.OnMessageSent -= ReceiveMessage;
    }

    void ReceiveMessage(string msg) {
        Debug.Log("Client received: " + msg);
    }
}
AClient received: Hello from Server
BServer received: Hello from Server
CNo output because events are not subscribed
DNullReferenceException at runtime
Attempts:
2 left
💡 Hint

Think about which class subscribes to the event and what message is passed.

🧠 Conceptual
intermediate
1:30remaining
Which statement best describes client-server roles in Unity networking?

In a Unity multiplayer game using client-server architecture, which statement is true about the roles of client and server?

AClients and server have identical responsibilities
BClients manage game state and server only relays messages
CThe server manages game state and clients send input commands
DServer only displays graphics, clients handle logic
Attempts:
2 left
💡 Hint

Think about who has authority over the game world.

🔧 Debug
advanced
2:30remaining
Why does this Unity client never receive server messages?

Given the following Unity code, why does the client never log the server's message?

Unity
public class Server : MonoBehaviour {
    public delegate void MessageHandler(string message);
    public static event MessageHandler OnMessageSent;

    void Start() {
        SendMessageToClient("Hello Client");
    }

    void SendMessageToClient(string msg) {
        OnMessageSent?.Invoke(msg);
    }
}

public class Client : MonoBehaviour {
    void Start() {
        Server.OnMessageSent += ReceiveMessage;
    }

    void ReceiveMessage(string msg) {
        Debug.Log("Received: " + msg);
    }
}
AEvent delegate is not static, so client can't subscribe
BClient subscribes too late; event fired before subscription
CClient unsubscribes immediately after subscribing
DServer never invokes the event
Attempts:
2 left
💡 Hint

Check the order of event subscription and invocation.

📝 Syntax
advanced
1:30remaining
Which option correctly declares a Unity event for client-server messaging?

Which of the following is the correct syntax to declare a static event in Unity C# for sending string messages from server to clients?

Apublic event static Action<string> OnMessageSent;
Bpublic static delegate event Action<string> OnMessageSent;
Cstatic public event void OnMessageSent(string message);
Dpublic static event Action<string> OnMessageSent;
Attempts:
2 left
💡 Hint

Remember the order: access modifier, static, event, delegate type, name.

🚀 Application
expert
2:00remaining
How many clients receive this server broadcast in Unity?

In a Unity server script, the server broadcasts a message to all connected clients using a static event. If 3 clients are connected and subscribed to the event, how many clients will receive the message?

Unity
using System;
using UnityEngine;

public class Server : MonoBehaviour {
    public static event Action<string> OnBroadcast;

    public void Broadcast(string msg) {
        OnBroadcast?.Invoke(msg);
    }
}

public class Client : MonoBehaviour {
    void OnEnable() {
        Server.OnBroadcast += Receive;
    }

    void OnDisable() {
        Server.OnBroadcast -= Receive;
    }

    void Receive(string msg) {
        Debug.Log("Client received: " + msg);
    }
}
A3
BDepends on network latency
C0
D1
Attempts:
2 left
💡 Hint

Think about how events work with multiple subscribers.

Practice

(1/5)
1. In Unity's client-server architecture, what is the main role of the client?
easy
A. Send requests to the server and receive responses
B. Store all game data permanently
C. Control the server hardware
D. Manage other clients directly

Solution

  1. Step 1: Understand client role in client-server

    The client initiates communication by sending requests to the server.
  2. Step 2: Understand server response

    The server processes requests and sends back responses to the client.
  3. Final Answer:

    Send requests to the server and receive responses -> Option A
  4. Quick Check:

    Client = Request sender [OK]
Hint: Clients always ask; servers always answer [OK]
Common Mistakes:
  • Thinking client stores all data permanently
  • Confusing client with server hardware control
  • Assuming clients manage other clients
2. Which of the following is the correct way to create a TcpClient in Unity C#?
easy
A. TcpClient client = TcpClient("127.0.0.1", 8080);
B. TcpClient client = new TcpClient.connectTo("127.0.0.1", 8080);
C. TcpClient client = new TcpClient.connect("127.0.0.1", 8080);
D. TcpClient client = new TcpClient("127.0.0.1", 8080);

Solution

  1. Step 1: Recall TcpClient constructor syntax

    In C#, creating a new TcpClient requires the 'new' keyword and constructor with IP and port.
  2. Step 2: Check each option's syntax

    Only TcpClient client = new TcpClient("127.0.0.1", 8080); uses 'new TcpClient("127.0.0.1", 8080);' correctly.
  3. Final Answer:

    TcpClient client = new TcpClient("127.0.0.1", 8080); -> Option D
  4. Quick Check:

    Use 'new' keyword with constructor [OK]
Hint: Always use 'new' to create objects in C# [OK]
Common Mistakes:
  • Omitting 'new' keyword
  • Using incorrect method names like connect or connectTo
  • Wrong constructor call syntax
3. What will the following Unity C# code output if the server sends back the string "Hello Client"?
using System;
using System.Net.Sockets;
using System.Text;

TcpClient client = new TcpClient("127.0.0.1", 9000);
NetworkStream stream = client.GetStream();
byte[] data = new byte[256];
int bytes = stream.Read(data, 0, data.Length);
string response = Encoding.ASCII.GetString(data, 0, bytes);
Console.WriteLine(response);
medium
A. System.Byte[]
B. Hello Client
C. null
D. Exception thrown

Solution

  1. Step 1: Understand data reading from NetworkStream

    The code reads bytes from the stream into a byte array and counts bytes read.
  2. Step 2: Convert bytes to string

    It converts the bytes read to a string using ASCII encoding, which matches the server's sent string.
  3. Final Answer:

    Hello Client -> Option B
  4. Quick Check:

    Read bytes + decode = server message [OK]
Hint: Decode bytes read from stream to get server message [OK]
Common Mistakes:
  • Printing byte array directly instead of decoding
  • Ignoring number of bytes read
  • Assuming null if no data
4. Identify the error in this Unity C# client code snippet:
TcpClient client = new TcpClient();
client.Connect("192.168.1.10", 7000);
NetworkStream stream = client.GetStream();
byte[] message = Encoding.ASCII.GetBytes("Ping");
stream.Write(message, 0, message.Length);
stream.Close();
client.Close();
medium
A. Missing try-catch block for network exceptions
B. Calling stream.Close() before client.Close() causes error
C. TcpClient must be created with IP and port in constructor
D. No error; code is correct

Solution

  1. Step 1: Review TcpClient creation and connection

    Creating TcpClient with default constructor and then calling Connect is valid.
  2. Step 2: Check for error handling

    Network operations can fail; missing try-catch means exceptions are unhandled.
  3. Final Answer:

    Missing try-catch block for network exceptions -> Option A
  4. Quick Check:

    Always handle network exceptions [OK]
Hint: Always wrap network calls in try-catch [OK]
Common Mistakes:
  • Assuming stream.Close() before client.Close() is wrong
  • Thinking TcpClient must have IP in constructor
  • Ignoring exception handling
5. You want to build a Unity client that connects to a server and sends a player's score only if it is above 1000. Which code snippet correctly implements this logic using TcpClient and NetworkStream?
hard
A. if(score > 1000) { TcpClient client = new TcpClient("server.com"); NetworkStream stream = client.GetStream(); byte[] data = Encoding.ASCII.GetBytes(score.ToString()); stream.Write(data, 0, data.Length); stream.Close(); client.Close(); }
B. TcpClient client = new TcpClient("server.com", 5000); NetworkStream stream = client.GetStream(); if(score > 1000) { byte[] data = Encoding.ASCII.GetBytes(score.ToString()); stream.Write(data, 0, data.Length); } stream.Close(); client.Close();
C. TcpClient client = new TcpClient(); if(score > 1000) { client.Connect("server.com", 5000); NetworkStream stream = client.GetStream(); byte[] data = Encoding.ASCII.GetBytes(score.ToString()); stream.Write(data, 0, data.Length); stream.Close(); client.Close(); }
D. if(score > 1000) { TcpClient client = new TcpClient(); client.Connect("server.com", 5000); NetworkStream stream = client.GetStream(); byte[] data = Encoding.ASCII.GetBytes(score); stream.Write(data, 0, data.Length); stream.Close(); client.Close(); }

Solution

  1. Step 1: Check conditional connection and sending

    TcpClient client = new TcpClient(); if(score > 1000) { client.Connect("server.com", 5000); NetworkStream stream = client.GetStream(); byte[] data = Encoding.ASCII.GetBytes(score.ToString()); stream.Write(data, 0, data.Length); stream.Close(); client.Close(); } creates TcpClient without parameters, connects only if score > 1000, then sends data.
  2. Step 2: Verify data conversion and method calls

    Converts score to string before encoding to bytes, uses correct Connect() and stream methods.
  3. Final Answer:

    new TcpClient(); client.Connect() inside if(score > 1000) -> Option C
  4. Quick Check:

    Connect and send inside if condition [OK]
Hint: Connect and send only inside the if block [OK]
Common Mistakes:
  • Sending data without converting score to string
  • Connecting outside the if condition
  • Using score directly as bytes without encoding
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