0
0
Unityframework~20 mins

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

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