- Home /
Question by
brandonhotdo · Aug 11, 2014 at 04:39 PM ·
c#nullreferenceexceptionreturnvoid
NullReference
I have gotten a null reference exception on line 10
using UnityEngine;
using System.Collections;
public class MainMenu : MonoBehaviour {
public int ServerPort = 25565;
public int MaxiPlayers = 27;
void OnGUI(){
if(GUI.Button(new Rect(Screen.width/2-310,Screen.height/2-175,183,28),"StartServer")){
NetworkManager2.Instance.StartServer(MaxiPlayers,ServerPort);
}
}
}
And on the script NetworkManager2
using UnityEngine;
using System.Collections;
public class NetworkManager2 : MonoBehaviour {
public static NetworkManager2 Instance;
// Use this for initialization
void Start () {
Instance = this;
}
public void StartServer(int MaxPlayers, int Port){
Debug.Log ("Good");
}
}
I don't know why I get the error! I heard that you can ignore it but then it does not do the void (It does not do what is in StartServer!).
Comment
Is your Network$$anonymous$$anager2 script attached to a game object?
Answer by gjf · Aug 11, 2014 at 04:44 PM
you should check that NetworkManager2.Instance
isn't null before you try to do anything with it.
instead of this:
if(GUI.Button(new Rect(Screen.width/2-310,Screen.height/2-175,183,28),"StartServer"))
{
NetworkManager2.Instance.StartServer(MaxiPlayers,ServerPort);
}
try this
if (NetworkManager2.Instance != null)
{
if(GUI.Button(new Rect(Screen.width/2-310,Screen.height/2-175,183,28),"StartServer"))
{
NetworkManager2.Instance.StartServer(MaxiPlayers,ServerPort);
}
}
you might also try setting Instance
in the Awake()
function instead of Start()
Oh the problem was I forgot to put the network manager 2 in the acuttle scene soz for wasting your time XD