Search Unity

Resolved Netcode.Singleton Always Returns NullReferenceException

Discussion in 'Netcode for GameObjects' started by luckygolden_, Dec 1, 2022.

  1. luckygolden_

    luckygolden_

    Joined:
    Sep 30, 2021
    Posts:
    9
    I tried programming a series of code using Netcode. However, trying to call
    NetworkManager.Singleton.StartHost();
    ,
    NetworkManager.Singleton.StartClient();
    and really anything even remotely similar to NetworkManager.Singleton.xxx does not work, and always returns a NullReferenceException.

    Does anyone else have this issue, and how can this be fixed? Thank you.
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,990
    Are you following documentation or a tutorial?
    If the singleton is null then NetworkManager hasn‘t been initialized. It may be missing from the scene or you are trying to use it too early (eg in Awake).
     
    RikuTheFuffs likes this.
  3. luckygolden_

    luckygolden_

    Joined:
    Sep 30, 2021
    Posts:
    9
    I tried that before but that didn't work. Thanks for attempting to help though! I will try using that advice to look further in and assess the issue in detail.
     
  4. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    440
    Hey @red_bunny0 , here's an example on how to wait until the Singleton is ready before you do something with it. Does it help?

    (The callbacks are just empty methods that match the signature of the event they are subscribed to)

    Code (CSharp):
    1. public class GameUI : MonoBehaviour
    2.     {
    3.         void OnEnable()
    4.         {
    5.             StartCoroutine(SubscribeToNetworkManagerEvents());
    6.         }
    7.  
    8.         IEnumerator SubscribeToNetworkManagerEvents()
    9.         {
    10.             yield return new WaitUntil(() => NetworkManager.Singleton);
    11.             NetworkManager.Singleton.OnClientConnectedCallback += OnClientConnectedCallback;
    12.             NetworkManager.Singleton.OnServerStarted += OnServerStarted;
    13.             NetworkManager.Singleton.OnClientDisconnectCallback += OnClientDisconnectCallback;
    14.         }
    15.  
    16.         void OnDestroy()
    17.         {
    18.             if (NetworkManager.Singleton)
    19.             {
    20.                 NetworkManager.Singleton.OnClientConnectedCallback -= OnClientConnectedCallback;
    21.                 NetworkManager.Singleton.OnServerStarted -= OnServerStarted;
    22.                 NetworkManager.Singleton.OnClientDisconnectCallback -= OnClientDisconnectCallback;
    23.             }
    24.         }
    25. }
     
  5. luckygolden_

    luckygolden_

    Joined:
    Sep 30, 2021
    Posts:
    9
    I never expected the official Unity team to respond, but hello there!

    Thanks for the help, I tried creating an IEnumerator that waits until the NetworkManager.Singleton is ready. But while it now doesn't throw in a NullReferenceException anymore, it still refused to do the action.

    So, I looked into the rest of the code, and it turns out that I shouldn't just call it from a script that occurs prior to the NetworkManager being open (in this case, the login authentication screen). So, I just put an OnStart() on the script responsible for automatically calling Hosts and Clients, and it worked! Thanks for your help, and I wish to see you around soon!
     
    RikuTheFuffs-U likes this.
  6. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    440
    hello, hello :D

    Happy to see that you found a fix for your specific case! See you around :D