Join a lobby

Players can specify a lobby ID or provide a lobby code to join a lobby. Lobby codes are short, auto-generated codes that map to specific lobbies and are intended for players to share with each other. The lobby code for a lobby is available as a members-only lobby property.

Best Practices

The Lobby service provides the best experience when requests are minimized. The Join API supports passing in player data as part of the Join call, so it's best to do that rather than making a Join call followed by a separate Update Player call after you've joined the lobby.

Join by specifying a lobby ID

The following code sample shows how to join a lobby by specifying a lobby ID:

C#

try
{
    await LobbyService.Instance.JoinLobbyByIdAsync("lobbyId");
}
catch (LobbyServiceException e)
{
    Debug.Log(e);
}

Join by providing a lobby code

The following code sample shows how to join a lobby with a code:

C#

try
{
    await LobbyService.Instance.JoinLobbyByCodeAsync("lobbyCode");
}
catch (LobbyServiceException e)
{
    Debug.Log(e);
}

Some text input packages may append invisible characters to strings from text fields that may result in an invalid join code being passed to the service. You will receive an InvalidJoinCode error with a detailed explanation as to why the code is invalid to help with this sort of error. You should also handle this error to notify users if they've entered an invalid character for a join.

Join a password-protected lobby

The following code sample shows how to join a password-protected lobby with an ID or code:

C#

try
{
    // Join by ID:
    var idOptions = new JoinLobbyByIdOptions{Password ="mySecret"};
    await Lobbies.Instance.JoinLobbyByIdAsync("lobbyId", idOptions);

    // Join by Code:
    var codeOptions = new JoinLobbyByCodeOptions{Password ="mySecret"};
    await Lobbies.Instance.JoinLobbyByCodeAsync("lobbyCode", codeOptions);
}
catch (LobbyServiceException e)
{
    Debug.Log(e);
}

Some text input packages may append invisible characters to strings from text fields that may result in an invalid password being passed to the service. The service is unable to determine whether these invisible characters are intentional or not so it may be impossible to differentiate this from a valid IncorrectPassword error. Be sure to verify that the password being sent in the request is the exact string you expect. The Join Lobby by Code API is able to provide more detailed information for errors like this so you may be able to verify the behavior of your UI using that API first.