Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Why cant i list lobbies?

Discussion in 'Multiplayer' started by Razorduck123, May 15, 2024.

  1. Razorduck123

    Razorduck123

    Joined:
    Apr 5, 2024
    Posts:
    3
    Hi! I am pretty new to unity and i am making a multiplayer game for steam. when I try to look for lobbies in my game I get this error:
    InvalidOperationException: Unable to get ILobbyServiceSdk because Lobby API is not initialized. Make sure you call UnityServices.InitializeAsync().
    Unity.Services.Lobbies.LobbyService.InitializeWrappedLobbyService () (at ./Library/PackageCache/com.unity.services.lobby@1.2.1/Runtime/SDK/LobbyService.cs:53)
    Unity.Services.Lobbies.LobbyService.get_Instance () (at ./Library/PackageCache/com.unity.services.lobby@1.2.1/Runtime/SDK/LobbyService.cs:34)
    Unity.Services.Lobbies.Lobbies.get_Instance () (at ./Library/PackageCache/com.unity.services.lobby@1.2.1/Runtime/SDK/LobbyService.cs:78)
    LobbyScript.ShowLobbies () (at Assets/Scripts/LobbyScript.cs:117)
    System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_0 (System.Object state) (at <27124aa0e30a41659b903b822b959bc7>:0)
    UnityEngine.UnitySynchronizationContext+WorkRequest.Invoke () (at <cb81df0c49c643b1a04d9fc6ccca2433>:0)
    UnityEngine.UnitySynchronizationContext.Exec () (at <cb81df0c49c643b1a04d9fc6ccca2433>:0)
    UnityEngine.UnitySynchronizationContext.ExecuteTasks (.
    Here is my code:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using Unity.Services.Core;
    using Unity.Services.Authentication;
    using TMPro;
    using Unity.Services.Lobbies;
    using Unity.Services.Lobbies.Models;
    using System.Threading.Tasks;
    using UnityEngine.UI;
    public class LobbyScript : MonoBehaviour
    {
    [Header("Lobby Creation")]
    [SerializeField] private TMP_InputField createLobbyNameField;
    [SerializeField] private TMP_InputField createLobbyMaxPlayersField;
    [SerializeField] private TMP_InputField JoiningBar;
    [SerializeField] private TMP_InputField LobbyName;
    [SerializeField] private TMP_InputField ProfileName;
    [SerializeField] private TextMeshProUGUI LobbyPeople;
    [SerializeField] private TMP_InputField JoinBar;
    [Space(10)]
    [Header("Lobby list")]
    [SerializeField] private GameObject lobbyListParent;
    [SerializeField] private Transform lobbyContentParent;
    [SerializeField] private Transform lobbyItemPrefab;
    [SerializeField] private TMP_InputField searchLobbyNameInputField;
    public float NumberOfLObbies;
    public string joinedLobbyId;
    private string JoinLobbyText;

    // Start is called before the first frame update
    private async void Start()
    {
    await UnityServices.InitializeAsync();
    await AuthenticationService.Instance.SignInAnonymouslyAsync();

    }
    public float PlayersinLobby;
    public async void CreateLobby()
    {
    if (!int.TryParse(createLobbyMaxPlayersField.text, out int maxPlayers))
    {
    Debug.LogWarning("Incorrect Player Count.");
    return;
    }
    Lobby createdlobby = null;
    PlayersinLobby = 1f;

    try
    {
    createdlobby = await LobbyService.Instance.CreateLobbyAsync(createLobbyNameField.text, maxPlayers);
    joinedLobbyId = createdlobby.Id;
    JoiningBar.text = createdlobby.LobbyCode;
    LobbyName.text = createdlobby.Name;
    LobbyPeople.text = createdlobby.Players.Count + "/" + maxPlayers;

    Debug.Log("Lobby Code " + createdlobby.LobbyCode);
    NumberOfLObbies = NumberOfLObbies + 1;
    } catch (LobbyServiceException e)
    {
    Debug.Log(e);
    }

    }

    public async void JoinLobbyByCode(string createdLobbyCode)
    {




    try
    {
    await Lobbies.Instance.JoinLobbyByCodeAsync(createdLobbyCode);
    Debug.Log("joined lobby " + createdLobbyCode);
    }
    catch (LobbyServiceException e)
    {
    Debug.Log(e);
    }

    }

    private async void ShowLobbies()
    {
    while (Application.isPlaying && lobbyListParent.activeInHierarchy)
    {
    QueryLobbiesOptions queryLobbiesOptions = new QueryLobbiesOptions();
    queryLobbiesOptions.Filters = new List<QueryFilter>();
    if (searchLobbyNameInputField.text != string.Empty)
    {
    queryLobbiesOptions.Filters.Add(new QueryFilter(QueryFilter.FieldOptions.Name, searchLobbyNameInputField.text, QueryFilter.OpOptions.CONTAINS));
    }
    QueryResponse queryResponse = await Lobbies.Instance.QueryLobbiesAsync(queryLobbiesOptions);
    foreach (Transform t in lobbyContentParent)
    {
    Destroy(t.gameObject);
    }
    foreach (Lobby lobby in queryResponse.Results)
    {
    Transform newLobbyItem = Instantiate(lobbyItemPrefab, lobbyContentParent);
    newLobbyItem.GetChild(0).GetComponent<TextMeshProUGUI>().text = lobby.Name;

    newLobbyItem.GetChild(1).GetComponent<TextMeshProUGUI>().text = lobby.Players.Count + "/" + lobby.MaxPlayers;
    }
    await Task.Delay(1000);
    }
    }
    public void JoinLobbyPressed()
    {
    ShowLobbies();
    }
    }
    (I am sorry i don't know how to do the code post thing.)
    I appreciate anyone's help!
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,922
    Did you, or did you not?
     
  3. Razorduck123

    Razorduck123

    Joined:
    Apr 5, 2024
    Posts:
    3