Search Unity

How to destroy lobby when load other scene?

Discussion in 'Netcode for GameObjects' started by MuteSummer, Dec 25, 2022.

  1. MuteSummer

    MuteSummer

    Joined:
    Dec 10, 2018
    Posts:
    2
    Hello, I m working on my 1st multiplayer game, so I m really new with it.

    I m developing game with only 2 scenes: Menu and Main scene. When user click Play button I load Main scene and start Lobby (if already exist any other lobby, then I m trying to join). But if user go back to Menu, I want to destroy Lobby at all, and player on other side should get OnClientDisconnectCallback.

    Now I see - if player go back to Menu there is nothing happen. If any of players turn off gmae at all, then OnClientDisconnectCallback will handle, but not if other scene loaded. How I can do it properly?

    For multiplayer I use UGS: Relay and Lobby

    This is my code for init my LobbyManager:

    Code (CSharp):
    1.         private void Awake()
    2.         {
    3.             if (SceneManager.currentScene == SceneHelper.MENU)
    4.             {
    5.  
    6.                 if (NetworkManager.Singleton != null)
    7.                 {
    8.                     NetworkManager.Singleton.Shutdown();
    9.                     Destroy(NetworkManager.Singleton.gameObject);
    10.                 }
    11.  
    12.                 return;
    13.             }
    14.          
    15.             LobbyManager._instance = this;      
    16.         }
    17.         private async void Start()
    18.         {
    19.             if (SceneManager.currentScene != SceneHelper.MAIN)
    20.                 return;
    21.  
    22.             // Initialize unity services
    23.             await UnityServices.InitializeAsync();
    24.  
    25.             // Setup events listeners
    26.             this.SetupEvents();
    27.  
    28.             // Unity Login
    29.             await SignInAnonymouslyAsync();
    30.  
    31.             // Subscribe to NetworkManager events
    32.             NetworkManager.Singleton.OnClientConnectedCallback += this.ClientConnected;
    33.             NetworkManager.Singleton.OnClientDisconnectCallback += this.ClientDisconnected;
    34.  
    35.             this.FindMatch();
    36.         }
     
    Last edited: Dec 25, 2022
  2. bbangerter

    bbangerter

    Joined:
    Sep 17, 2014
    Posts:
    7
    Add a
    private void OnDestroy()
    {
    .. call lobby shutdown code...
    }

    to one of the behaviors in your game scene.
     
  3. MuteSummer

    MuteSummer

    Joined:
    Dec 10, 2018
    Posts:
    2
    Hi, this is my OnDestroy code:
    Code (CSharp):
    1. private async void OnDestroy()
    2.         {
    3.             // We need to delete the lobby when we're not using it
    4.             Debug.Log("Destroying");
    5.            
    6.             NetworkManager.Singleton.Shutdown();
    7.            
    8.             string playerId = AuthenticationService.Instance.PlayerId;
    9.             await LobbyService.Instance.RemovePlayerAsync(_lobbyId, playerId);
    10.             await Lobbies.Instance.DeleteLobbyAsync(_lobbyId);
    11.  
    12.             AuthenticationService.Instance.SignOut();
    13.         }
    Unfortunately, still not working :(
     
  4. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    440
    Hi @MuteSummer , I'm not sure you can make OnDestroy async, given that it's a magic method called by the engine,

    Does changing you script in this way solve your issue?


    Code (CSharp):
    1. void OnDestroy()
    2.         {
    3.              DestroyLobby();
    4.         }
    5.  
    6. async void DestroyLobby()
    7. {
    8.             // We need to delete the lobby when we're not using it
    9.             Debug.Log("Destroying");
    10.          
    11.             NetworkManager.Singleton.Shutdown();
    12.          
    13.             string playerId = AuthenticationService.Instance.PlayerId;
    14.             await LobbyService.Instance.RemovePlayerAsync(_lobbyId, playerId);
    15.             await Lobbies.Instance.DeleteLobbyAsync(_lobbyId);
    16.  
    17.             AuthenticationService.Instance.SignOut();
    18. }
    19.  
    20.