Mirror NetworkManager null reference by other component in GameObject

Hi - I’m trying to create a two-player game and I’m trying to create a component for my NetworkManager GameObject that references the NetworkManager component. I have something like this:

    [RequireComponent(typeof(NetworkManager))]
    public class GameController : MonoBehaviour {

        NetworkManager networkManager;

        private void Awake() {
            networkManager = GetComponent<NetworkManager>();
        }

        public bool CheckNumPlayers() { // called by button
            return networkManager.numPlayers == 2;
        }
...

However, when I call CheckNumPlayers, I get an error saying NullReferencePointer. If I call GetComponent<NetworkManager>().numPlayers directly, it gets the reference and works as expected, but I plan on using the NetworkManager component quite a bit and I don’t want to have to Get it every time. Is there any reason the reference is getting lost that I’m missing?

Why not just make networkManager public and just drag it via the inspector?

Add a Debug.Log to Awake to verify it is being called.

1 Like

It is definitely being called in the Awake and it gets defined there. I had a Debug.Log() message and it showed the reference was received and set.

I’ll give the inspector approach a try, for some reason I hadn’t thought of that.