NetworkManager.singleton is null

Hi,
I’m following this tutorial on creating a custom NetworkManager

At 1:35 he uses:

NetworkManager.singleton.StartHost();

I thought I’d followed the same steps but my “singleton” is null. Am I missing a step?

Thanks.

I’m getting the same issue with my own project. Bump!

With the current version of Unity (5.2.x), if you override the NetworkManager and have an Awake() function in your override class, the NetworkManager’s own Awake() function will not be called and the singleton won’t be set… I hope that was your issue!

5 Likes

Had that issue a few days back Unity Issue Tracker - Derived NetworkManager with Awake method breaks NetworkManager if loading scenes

Someone also posted a workaround that uses reflection to bypass Awake’s access modifiers

Or… you can just use the Start() method instead :wink:

By the way, by adding the following code to your own overriden Awake(), you basically replace it’s functionality and get rid of that warning:

       LogFilter.currentLogLevel = (int)logLevel;
      
        if (instance != null && instance != this)
        {
            Destroy(gameObject);
            return;
        }
          
        DontDestroyOnLoad(gameObject);
      
        NetworkManager.singleton = this;

The bug report had additional text which got cut, if you implement Awake and you’re using Online/Offline scenes the NetworkManager silently stops working(doesn’t spawn player prefab or anything else). If it was obvious we’re doing something bad by implementing Awake() of course people would find alternatives. However there are no compiler warnings since it’s not marked virtual (not sure which warning you’re talking of, VS 2015 outputs no warnings), there are no console messages even if you crank up LogLevel to developer, and it’s tricky to figure out what’s happening unless you use a reflector on the .DLL or explicitly look up the singleton’s value using debugger

IMO reflection solution is better than duplicating Awake code, as it doesn’t rely on unity keeping the same Awake() method when/if they get around to fixing this

It says this bug is fixed in 5.3.0 but I’m using 5.3.1p2 and the problem persists. Looking at the publicly available code in BitBucket, it appears Awake() is non-virtual.