Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question Connecting twice to a game

Discussion in 'Netcode for GameObjects' started by lavagoatGG, Mar 28, 2023.

  1. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    229
    If I connect to a game, Then disconnect from it and try to connect again, the in-scene network objects won't spawn again. This is my disconnection code:
    Code (CSharp):
    1. try
    2.         {
    3.             if (NetworkManager.Singleton.IsServer)
    4.             {
    5.                 await LobbyService.Instance.DeleteLobbyAsync(lobby.Id);
    6.             }
    7.             else
    8.             {
    9.                 await LobbyService.Instance.RemovePlayerAsync(lobby.Id, playerId);
    10.             }
    11.             Debug.Log("sucssefully dissconected from lobby");
    12.             //shutdown
    13.             try
    14.             {
    15.  
    16.                 NetworkManager.Shutdown();
    17.                 NetworkManager networkManager = GameObject.FindObjectOfType<NetworkManager>();
    18.                 Destroy(networkManager.gameObject);
    19.                 Debug.Log("Network manager was shut down succesfuly");
    20.             }
    21.             catch
    22.             {
    23.                 Debug.Log("Network manager already shut down");
    24.             }
    25.  
    26.         }
    27.         catch (LobbyServiceException e)
    28.         {
    29.             Debug.Log(e);
    30.         }
    31.         SceneManager.LoadScene(0);
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    7,000
    Code (CSharp):
    1. Destroy(networkManager.gameObject);
    You're not supposed to destroy the NetworkManager object. It's in the "DontDestroyOnLoad" subscene for a reason! ;)
     
  3. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    229
    Yes but if I don't destroy it I have two of them and then I can't even connect to a game. I will check If it is fixed in the new version
     
  4. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    229