Search Unity

Bug When players try to join a match, only the client enters but not the host

Discussion in 'Netcode for GameObjects' started by thelaughingzasshu, Jan 6, 2024.

  1. thelaughingzasshu

    thelaughingzasshu

    Joined:
    Jan 1, 2024
    Posts:
    12
    This is my player lobby code

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Unity.Services.Core;
    5. using Unity.Services.Authentication;
    6. using Unity.Services.Lobbies;
    7. using Unity.Services.Lobbies.Models;
    8. using Unity.Netcode;
    9. using UnityEngine.SceneManagement;
    10. using UnityEngine.UIElements;
    11.  
    12. public class PlayerLobby : MonoBehaviour
    13. {
    14.     private Lobby joinedLobby;
    15.     private float heartbeat;
    16.     public static PlayerLobby Instance { get; private set; }
    17.     public static NetworkManager Singleton { get; private set; }
    18.     private bool loading = false;
    19.  
    20.     private void Awake()
    21.     {
    22.         if (Instance != null && Instance != this) Destroy(gameObject);
    23.         else
    24.         {
    25.             Instance = this;
    26.             DontDestroyOnLoad(gameObject);
    27.         }
    28.     }
    29.  
    30.     // Start is called before the first frame update
    31.     async void Start()
    32.     {    
    33.         if (UnityServices.State != ServicesInitializationState.Initialized)
    34.         {
    35.             InitializationOptions options = new InitializationOptions();
    36.             options.SetProfile(Random.Range(0,9999999).ToString());
    37.  
    38.             await UnityServices.InitializeAsync(options);
    39.             AuthenticationService.Instance.SignedIn += () =>
    40.             {
    41.                 Debug.Log(AuthenticationService.Instance.PlayerId);
    42.             };
    43.             await AuthenticationService.Instance.SignInAnonymouslyAsync();
    44.         }    
    45.     }
    46.  
    47.     // Update is called once per frame
    48.     void Update()
    49.     {
    50.         HandleHeartbeat();
    51.  
    52.         if (loading)
    53.         {
    54.             Debug.Log(joinedLobby.Players.Count);
    55.             if (joinedLobby != null && joinedLobby.Players.Count == joinedLobby.MaxPlayers)
    56.             {
    57.                 SceneManager.LoadScene("Arena", LoadSceneMode.Single);
    58.                 if (Ishost(joinedLobby))
    59.                 {
    60.                     NetworkManager.Singleton.StartHost();
    61.                 }
    62.                 else
    63.                 {
    64.                     NetworkManager.Singleton.StartClient();
    65.                 }
    66.  
    67.                 NetworkManager.Singleton.OnClientDisconnectCallback += OnClientDisconnect;
    68.                 loading = false;
    69.             }
    70.         }
    71.     }
    72.  
    73.     public async void HandleHeartbeat()
    74.     {
    75.         if (joinedLobby != null && Ishost(joinedLobby))
    76.         {
    77.             heartbeat -= Time.deltaTime;
    78.  
    79.             if (heartbeat < 0f)
    80.             {
    81.                 float heartbeatmax = 15f;
    82.                 heartbeat = heartbeatmax;
    83.  
    84.                 await LobbyService.Instance.SendHeartbeatPingAsync(joinedLobby.Id);
    85.             }
    86.         }
    87.     }
    88.  
    89.     public async void CreateLobby(bool privated)
    90.     {    
    91.         CreateLobbyOptions options = new CreateLobbyOptions
    92.         {
    93.             IsPrivate = privated,
    94.         };
    95.  
    96.         Lobby lobby = await LobbyService.Instance.CreateLobbyAsync("Goat", 2, options);
    97.         await Lobbies.Instance.JoinLobbyByIdAsync(lobby.Id);
    98.         joinedLobby = lobby;
    99.         Debug.Log(joinedLobby.LobbyCode);
    100.     }
    101.  
    102.     public async void JoinLobby(string mode, string code)
    103.     {
    104.         if (string.IsNullOrEmpty(code)) return;
    105.  
    106.         try
    107.         {
    108.             if (mode == "id") joinedLobby = await Lobbies.Instance.JoinLobbyByIdAsync(code);
    109.             else if (mode == "code") joinedLobby = await Lobbies.Instance.JoinLobbyByCodeAsync(code);
    110.          
    111.         } catch (LobbyServiceException ex) {
    112.             Debug.Log(ex);
    113.         }    
    114.     }
    115.  
    116.     public async void QuickJoinLobby()
    117.     {
    118.         try
    119.         {
    120.             QueryResponse query = await Lobbies.Instance.QueryLobbiesAsync(new QueryLobbiesOptions
    121.             {
    122.                 Filters = new List<QueryFilter>
    123.                     {
    124.                         new QueryFilter(
    125.                             field: QueryFilter.FieldOptions.AvailableSlots,
    126.                             op: QueryFilter.OpOptions.GT,
    127.                             value: "0")
    128.                     },
    129.                 Order = new List<QueryOrder>
    130.                     {
    131.                         new QueryOrder(true, QueryOrder.FieldOptions.Created),
    132.                     }
    133.             });
    134.  
    135.             if (query.Results.Count == 0)
    136.             {
    137.                 CreateLobby(false);
    138.                 Debug.Log("Lobby created");
    139.             }
    140.             else
    141.             {
    142.                 JoinLobby("id", query.Results[0].Id);
    143.                 Debug.Log("Lobby joined");
    144.             }
    145.  
    146.             loading = true;
    147.         }
    148.         catch (LobbyServiceException ex)
    149.         {
    150.             Debug.Log(ex);
    151.         }
    152.     }
    153.  
    154.     private bool Ishost(Lobby lobby)
    155.     {
    156.         return lobby.HostId == AuthenticationService.Instance.PlayerId;
    157.     }
    158.  
    159.     private void OnClientDisconnect(ulong id)
    160.     {
    161.         LeaveLobby();
    162.     }
    163.  
    164.     public async void LeaveLobby()
    165.     {
    166.         try
    167.         {
    168.             await LobbyService.Instance.DeleteLobbyAsync(joinedLobby.Id);
    169.             joinedLobby = null;
    170.             SceneManager.LoadScene("Menu", LoadSceneMode.Single);
    171.         }
    172.         catch (LobbyServiceException ex)
    173.         {
    174.             Debug.Log(ex);
    175.         }
    176.     }
    177.  
    178.     public async void ListLobbies()
    179.     {
    180.         QueryResponse query = await Lobbies.Instance.QueryLobbiesAsync();
    181.     }
    182. }
    183.  
    When a client joins the lobby, the code in the update function will run, and the player will enter the next scene. However, this is not the case for the lobby host itself, as the host's joinedLobby.Players.Count gets stuck at 1, instead of 2. I have put this question in the Lobby part of this forum but no one responded. I also searched Google for answers but it can't give a clear direction. Any ideas on this? Thank you!