Problems with direct connection using Lobby

Hello Developers,

I’m having trouble creating a simple direct join connection using Lobby (and Relay).

With the function CreateLobby(), I can create a Lobby and show it with a UI-Text.

With the function FindLobby(), I can find and quickjoin to an open Lobby.

Now I want to directly join a Lobby using an InputField and the JoinLobby()-Function but the error states:

[Lobby]: LobbyNotFound, (16001). Message: lobby not found
UnityEngine.Logger:LogError (string,object)

I try to manually put the ID in the InputField and even Debug.Log the InputField to make sure it is the same ID but it just won’t connect. And when I quickjoin, it does work. Here is the code:

public async void CreateLobby()
    {
        Debug.Log("Creating a new lobby...");

        // External connections
        int maxConnections = 1;
        try
        {
            Allocation allocation = await Relay.Instance.CreateAllocationAsync(maxConnections);
            _hostData = new RelayHostData
            {
                Key = allocation.Key,
                Port = (ushort)allocation.RelayServer.Port,
                AllocationID = allocation.AllocationId,
                AllocationIDBytes = allocation.AllocationIdBytes,
                ConnectionData = allocation.ConnectionData,
                IPv4Address = allocation.RelayServer.IpV4
            };

            // Retrieve JoinCode
            _hostData.JoinCode = await Relay.Instance.GetJoinCodeAsync(allocation.AllocationId);


            string lobbyName = "game_lobby";
            int maxPlayers = 10;
            CreateLobbyOptions options = new CreateLobbyOptions();
            options.IsPrivate = false;
           

            // Put the JoinCode in the lobby data, visible by every member
            options.Data = new Dictionary<string, DataObject>()
            {
                {
                    "joinCode", new DataObject(
                        visibility: DataObject.VisibilityOptions.Member,
                        value: _hostData.JoinCode)
                },
            };

            // Create the lobby
            Lobby lobby = await LobbyService.Instance.CreateLobbyAsync(lobbyName, maxPlayers, options);
           
            // Show the LobbyID in UI
           
            lobbyText.GetComponent<TMPro.TMP_Text>().SetText(lobby.Id);

            Debug.Log(_hostData.JoinCode);
            Debug.Log("Created lobby: " + lobby.Id);

            // Heartbeat the lobby every 15 seconds.
            stopheartbeat = false;

            if(stopheartbeat == false)  {
                StartCoroutine(HeartbeatLobbyCoroutine(lobby.Id, 15));
            }
            if(stopheartbeat == true) {
                StopCoroutine(HeartbeatLobbyCoroutine(lobby.Id, 15));
            }

            NetworkManager.Singleton.GetComponent<UnityTransport>().SetRelayServerData(
                _hostData.IPv4Address,
                _hostData.Port,
                _hostData.AllocationIDBytes,
                _hostData.Key,
                _hostData.ConnectionData);

            NetworkManager.Singleton.StartHost();

        }
        catch (LobbyServiceException e)
        {
            Console.WriteLine(e);
            throw;
        }
    }

public async void FindLobby()
    {
            Debug.Log("Looking for a lobby...");

            try
            {
               
                QuickJoinLobbyOptions options = new QuickJoinLobbyOptions();

                Lobby lobby = await LobbyService.Instance.QuickJoinLobbyAsync(options);


                Debug.Log("Joined lobby: " + lobby.Id);
                Debug.Log("Lobby Players: " + lobby.Players.Count);

                string joinCode = lobby.Data["joinCode"].Value;

                Debug.Log("Received code: " + joinCode);

                JoinAllocation allocation = await Relay.Instance.JoinAllocationAsync(joinCode);

                _joinData = new RelayJoinData
                {
                    Key = allocation.Key,
                    Port = (ushort)allocation.RelayServer.Port,
                    AllocationID = allocation.AllocationId,
                    AllocationIDBytes = allocation.AllocationIdBytes,
                    ConnectionData = allocation.ConnectionData,
                    HostConnectionData = allocation.HostConnectionData,
                    IPv4Address = allocation.RelayServer.IpV4
                };

                NetworkManager.Singleton.GetComponent<UnityTransport>().SetRelayServerData(
                    _joinData.IPv4Address,
                    _joinData.Port,
                    _joinData.AllocationIDBytes,
                    _joinData.Key,
                    _joinData.ConnectionData,
                    _joinData.HostConnectionData);

                NetworkManager.Singleton.StartClient();
            }
            catch (LobbyServiceException e)
            {
                Debug.Log("Cannot find a lobby: " + e);
            }
    }

public async void JoinLobby()
    {
             try
        {
            JoinLobbyByIdOptions options = new JoinLobbyByIdOptions();

            Lobby lobby = await LobbyService.Instance.JoinLobbyByIdAsync(joinCodeInputFieldText.GetComponent<TMPro.TMP_Text>().text, options);

//...nothing changes from here with FindLobby() after QuickJoinAsync()
           }
}

This had come up a few times here is the solution that worked for the last few users.

Let me know if this resolves your issue and I will try to get this added to the FAQ or add a warning about it in the docs.

1 Like

OMG, Thank you so much, I was stuck for 6 hours figuring things out and it was a simple fix. For everyone, who has the same problem:

  1. Make sure to drag the right Game Object in the SerializeField (The Input Field and not the text of the input field)
  2. Use the GetComponent<TMPro.TMP_Inputfield>() like in my code below
[SerializeField] private GameObject joinCodeInputField;
...

public async void JoinLobby()
    {
        Debug.Log("Looking for a lobby...");

        try
        {
            JoinLobbyByCodeOptions options = new JoinLobbyByCodeOptions();

            Lobby lobby = await LobbyService.Instance.JoinLobbyByCodeAsync(joinCodeInputField.GetComponent<TMPro.TMP_InputField>().text, options);
...}

Thank you again!