Network Manager being null on build but not editor

I have a working host/client lobby system in a menu and am working on connecting that to the main game.

As a check point for myself I made a build of what I currently have just for funsies and noticed that my lobby does not work on the built version of the game.

After enabling some settings and checking the logs it seems that in my build, my Network Manager is null by the time I get to the main menu and returns an exception when I try to access the singleton.

My project has the following scene structure:

Black screen with Network Manager → Main Menu → Main game

The first scene just loads Network Manager then loads the Main Menu, this is so that I can return to the Main Menu later without duplicating my Network Managers since they are set to “DontDestroyOnLoad”.

`

If this is the case, any ideas on how to go about investigating this?

I did see a older discussion from 2015 that clarified some bug that happens when you try to override the Network Manager’s “Awake” function but I am not doing that.

Any advice or ideas would be greatly, greatly appreciated!

Cheers.

Strange that it would be null with your setup. After all it should be in the DDOL scene once you get to Main Menu thus there cannot be any order of execution issue. NM singleton may be null in Awake in the “black screen” scene but no longer in Main Menu.

At what point do you change to the Main Menu scene? If I recall correctly this has to be done in Update or at the earliest in Start, so it couldn’t possibly load the Main Menu before the NetworkManager has initialized and put itself into DDOL. I believe that happens during OnEnable.

In any case, delay loading the main menu scene with a coroutine (after yield null) to rule this out as an issue.

And in Main Menu, just for testing, try a GameObject.Find to see if it’ll find the NM object. It could potentially get destroyed by some other script.

There is no additional NM in the Main Menu or other scenes? In case the duplication happens in other scenes.

You may want to post the code where the nullref occurs and the error message including callstack (see player.log).

 private void SetupHostLobby(bool success, string joinCode)
 {
     if (NetworkManager.Singleton != null)
         Debug.Log("-------------------------Network Manager singleton not null---------------------------");
     else
         Debug.Log("-------------------------Network Manager singleton is null---------------------------");

     try
     {
         string name = GameObject.Find("NetworkManager").name;
         Debug.Log($"-------------------------NetworkManager object found: {name}-------------------------");
     }
     catch(Exception e)
     {
         Debug.Log("-------------------------Network Manager object is null-------------------------");
     }

     if (success)
     {
         NetworkManager.Singleton.OnClientDisconnectCallback += OnClientDisconnect;
         //SetupHostUI();
         joinCodeObject.transform.GetChild(0).GetComponent<TextMeshProUGUI>().text = joinCode;
     }
     else
     {
         SetupInitialUI();
         Debug.Log("Lobby failed to create!");
     }
 }

Above is the code for setting up a host lobby (this is what I am using to test)

Attached is the player log file from my last test. I tried loading the scene using a coroutine as you suggested and also made sure there are no duplicates of NM, the only one is in the initial black screen. Additionally, I also tried GameObject.Find and it confirmed that there is no NM in the Main Menu scene.

Playerlog.txt (65.5 KB)

You may want to add a custom component on the same object as NetworkManager. This component simply logs when OnDestroy runs. This is to check if the object gets destroyed - if it is, that’s an issue you need to locate. The NetworkManager object shouldn’t get destroyed.

I just vaguely recalled that GameObject.Find may not find objects under DDOL. I think finding DDOL objects isn’t actually that simple.

Ok so I’ve also implemented that suggestion and it appears the Network Manager does NOT get destroyed, but still manages to be null…

ALRIGHT!!!

So after some more days of digging, I have come to the embarrassing solution:

Turns out that I had my scenes in the wrong order and thus I was not loading the black screen first, I was loading the MainMenu first.

TLDR;

For anyone seemingly having the same issue, check your scene order in the build settings

1 Like