Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Bug NetworkManager NullReferenceException

Discussion in 'Netcode for GameObjects' started by rojenarda, Aug 30, 2023.

  1. rojenarda


    Jun 7, 2023
    Edit: I solved the problem by instantiating network manager in a different scene only once at the beginning of the game and never destroying it afterwards, apparently you don't need to instantiate network manager after Shutdown(), that it why I was destroying and creating it again. But I still don't understand why I get a NullReferenceException after creating it for the second time.

    I have problem with NetworkManager when I destroy it and re-initialize it, I get a NullReferenceException. I use a cleanup scene that destroys network manager (and other singletons I use) when returning to main menu scene, where all these singletons are being initialized. Whenever a players leaves a lobby or the game, they load the clenup scene first, then it redirects to the main menu scene. Leaving sequence works as follows:
    1. Hit leave button, call NetworkManager.Singleton.Shutdown
    2. Load cleanup scene, if NetworkManager.Singleton is not null, destroy its game object
    3. Load main menu scene, create network manager game object and other singletons
    The problem is that after this if I try to use NetworkManager again, it throws NullReferenceException, but NetworkManager gameobject is present in the scene and it is enabled, am I doing something wrong? Should I not call Shutdown when a player leaves the lobby or game? Or is there something I should do other than spawning the NetworkManager game object to initialize NetworkManager (although it is initialized correctly on the first time)
    Last edited: Aug 31, 2023
    MUK-Dev likes this.