Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

NetworkManager.singleton is null

Discussion in 'Multiplayer' started by NotQuiteSmith, Sep 17, 2015.

  1. NotQuiteSmith

    NotQuiteSmith

    Joined:
    Oct 27, 2013
    Posts:
    92
    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.
     
  2. Deleted User

    Deleted User

    Guest

    I'm getting the same issue with my own project. Bump!
     
  3. _Adriaan

    _Adriaan

    Joined:
    Nov 12, 2009
    Posts:
    481
    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!
     
  4. irwiss

    irwiss

    Joined:
    Apr 14, 2015
    Posts:
    7
  5. _Adriaan

    _Adriaan

    Joined:
    Nov 12, 2009
    Posts:
    481
    Or.... you can just use the Start() method instead ;)
     
  6. _Adriaan

    _Adriaan

    Joined:
    Nov 12, 2009
    Posts:
    481
    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:

    Code (CSharp):
    1.        LogFilter.currentLogLevel = (int)logLevel;
    2.      
    3.         if (instance != null && instance != this)
    4.         {
    5.             Destroy(gameObject);
    6.             return;
    7.         }
    8.          
    9.         DontDestroyOnLoad(gameObject);
    10.      
    11.         NetworkManager.singleton = this;
     
  7. irwiss

    irwiss

    Joined:
    Apr 14, 2015
    Posts:
    7
    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
     
  8. Jodon

    Jodon

    Joined:
    Sep 12, 2010
    Posts:
    434
    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.