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?
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); } }
Think about which class subscribes to the event and what message is passed.
The Server class sends a message via an event. The Client subscribes to this event and logs the received message. So the output is the client logging the server's message.
In a Unity multiplayer game using client-server architecture, which statement is true about the roles of client and server?
Think about who has authority over the game world.
The server is authoritative and manages the game state. Clients send their input to the server, which processes and updates the state.
Given the following Unity code, why does the client never log the server's message?
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); } }
Check the order of event subscription and invocation.
The Server fires the event in Start(), but the Client subscribes in its Start(), which may run after the Server's Start(). So the event is fired before the client subscribes.
Which of the following is the correct syntax to declare a static event in Unity C# for sending string messages from server to clients?
Remember the order: access modifier, static, event, delegate type, name.
Option D correctly declares a static event using the Action delegate with a string parameter.
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?
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); } }
Think about how events work with multiple subscribers.
Each subscribed client receives the event invocation, so all 3 clients get the message.