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?