0
0
Unityframework~20 mins

Lobby and matchmaking basics in Unity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Lobby and Matchmaking Master
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 lobby player count code?

Consider this Unity C# code snippet that manages a lobby's player count. What will be printed to the console?

Unity
using UnityEngine;
using System.Collections.Generic;

public class LobbyManager : MonoBehaviour {
    private List<string> players = new List<string>();

    void Start() {
        players.Add("Alice");
        players.Add("Bob");
        players.Remove("Alice");
        Debug.Log($"Players in lobby: {players.Count}");
    }
}
APlayers in lobby: 1
BPlayers in lobby: 2
CPlayers in lobby: 0
DPlayers in lobby: 3
Attempts:
2 left
💡 Hint

Think about how many players are added and removed before the count is printed.

🧠 Conceptual
intermediate
1:30remaining
Which statement best describes matchmaking in Unity?

Matchmaking helps players find games to join. Which option correctly describes its main purpose?

AIt matches players based on criteria like skill or region to create balanced games.
BIt connects players to random game servers without any filtering.
CIt only allows players to join games created by their friends.
DIt automatically creates new game servers for every player joining.
Attempts:
2 left
💡 Hint

Think about why matchmaking is important for player experience.

🔧 Debug
advanced
2:30remaining
Why does this lobby join code cause a runtime error?

Look at this code snippet that tries to join a lobby. What causes the runtime error?

Unity
using UnityEngine;

public class LobbyJoiner : MonoBehaviour {
    string lobbyId = null;

    void JoinLobby() {
        Debug.Log($"Joining lobby {lobbyId.ToUpper()}");
    }

    void Start() {
        JoinLobby();
    }
}
AThe lobbyId variable is not initialized with a valid lobby name.
BThe method JoinLobby is private and cannot be called from Start.
CDebug.Log cannot use string interpolation in Unity.
DlobbyId is null, so calling ToUpper() causes a NullReferenceException.
Attempts:
2 left
💡 Hint

Check what happens when you call a method on a null string.

📝 Syntax
advanced
1:30remaining
Which option correctly declares a lobby dictionary with player counts?

You want to create a dictionary in C# that maps lobby names (strings) to player counts (integers). Which declaration is correct?

ADictionary lobbies = new Dictionary<string, int>();
BDictionary<string, int> lobbies = new Dictionary<string, int>();
CDictionary<string> lobbies = new Dictionary<string>();
DDictionary<int, string> lobbies = new Dictionary<int, string>();
Attempts:
2 left
💡 Hint

Remember the syntax for generic dictionaries in C#.

🚀 Application
expert
3:00remaining
What is the final player count after this matchmaking simulation?

Given this code simulating players joining and leaving a lobby, what is the final count printed?

Unity
using System.Collections.Generic;
using UnityEngine;

public class MatchmakingSim : MonoBehaviour {
    List<string> players = new List<string>();

    void Simulate() {
        players.Add("Anna");
        players.Add("Ben");
        players.Add("Cara");
        players.RemoveAt(1);
        players.Add("Dan");
        players.Remove("Anna");
        Debug.Log($"Final players: {players.Count}");
    }

    void Start() {
        Simulate();
    }
}
AFinal players: 4
BFinal players: 1
CFinal players: 2
DFinal players: 3
Attempts:
2 left
💡 Hint

Track each add and remove step carefully.