A lobby lets players gather before a game starts. Matchmaking helps find players to play together or against each other.
Lobby and matchmaking basics in Unity
using UnityEngine; using UnityEngine.Networking; public class LobbyManager : NetworkLobbyManager { // Override methods to customize lobby behavior public override void OnLobbyClientEnter() { Debug.Log("Player joined the lobby"); } public override GameObject OnLobbyServerCreateLobbyPlayer(NetworkConnection conn, short playerControllerId) { Debug.Log("Creating lobby player"); return base.OnLobbyServerCreateLobbyPlayer(conn, playerControllerId); } }
Unity uses NetworkLobbyManager to handle lobby and matchmaking basics.
You can override methods to customize what happens when players join or leave.
public override void OnLobbyClientEnter()
{
Debug.Log("A new player has entered the lobby.");
}public override GameObject OnLobbyServerCreateLobbyPlayer(NetworkConnection conn, short playerControllerId)
{
Debug.Log("Lobby player created.");
return base.OnLobbyServerCreateLobbyPlayer(conn, playerControllerId);
}This simple lobby manager logs when players join and starts the game when all are ready.
using UnityEngine; using UnityEngine.Networking; public class SimpleLobby : NetworkLobbyManager { public override void OnLobbyClientEnter() { Debug.Log($"Player connected: {NetworkClient.connection.connectionId}"); } public override void OnLobbyServerPlayersReady() { Debug.Log("All players are ready. Starting game..."); ServerChangeScene(playScene); } }
Matchmaking often requires a server or service to connect players.
Unity's built-in networking is deprecated; consider using newer solutions like Mirror or Unity Relay.
Always test lobbies with multiple players to ensure smooth joining and starting.
Lobbies let players wait and prepare before a game.
Matchmaking finds and connects players automatically.
Override lobby methods to customize player joining and game start.
Practice
Solution
Step 1: Understand the role of a lobby
A lobby is a waiting area where players gather before the game begins.Step 2: Differentiate from matchmaking
Matchmaking is the process of finding and connecting players automatically, not the lobby itself.Final Answer:
To let players wait and prepare before the game starts -> Option AQuick Check:
Lobby = waiting area before game [OK]
- Confusing lobby with matchmaking
- Thinking lobby handles graphics
- Assuming lobby saves progress
Solution
Step 1: Recall C# override syntax
In C#, the correct syntax places 'override' before the return type.Step 2: Check each option
public override void OnPlayerJoined() {} uses 'public override void MethodName()' which is correct syntax.Final Answer:
public override void OnPlayerJoined() {} -> Option AQuick Check:
Override keyword before return type [OK]
- Placing override after method name
- Omitting override keyword
- Wrong order of keywords
public override void OnPlayerJoined(Player player) {
Debug.Log($"Player {player.Name} joined.");
if (Players.Count >= 4) {
StartGame();
}
}What happens when the 4th player joins?
Solution
Step 1: Analyze the OnPlayerJoined method
When a player joins, it logs their name and checks if Players.Count is 4 or more.Step 2: Understand the condition
If Players.Count is 4 or more, StartGame() is called, starting the game automatically.Final Answer:
The game starts automatically -> Option CQuick Check:
Players.Count >= 4 triggers StartGame() [OK]
- Assuming manual start needed
- Thinking Players.Count is undefined
- Confusing lobby closing with game start
public void Matchmake() {
var match = Matchmaker.CreateMatch();
match.OnPlayerJoined += OnPlayerJoined;
match.Start();
}
void OnPlayerJoined(Player player) {
Debug.Log("Player joined: " + player.Name);
}Solution
Step 1: Review event subscription
The syntax 'match.OnPlayerJoined += OnPlayerJoined;' is correct for subscribing to events.Step 2: Check for null safety
Matchmaker.CreateMatch() might return null if no match is found, but code does not check for null before using match.Final Answer:
Matchmaker.CreateMatch() might return null and is not checked -> Option DQuick Check:
Always check for null after CreateMatch() [OK]
- Assuming event subscription syntax is wrong
- Ignoring null checks
- Assuming Start() method is missing
Solution
Step 1: Understand the requirement
The game should start only when all players are ready, not just when they join.Step 2: Choose the best method
Overriding OnPlayerJoined to check each player's 'ready' status before starting the game ensures all are prepared.Final Answer:
Override OnPlayerJoined to check a 'ready' flag on each player before calling StartGame() -> Option BQuick Check:
Check readiness before starting game [OK]
- Starting game too early
- Ignoring player readiness
- Disabling matchmaking unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
