NullReferenceException: Object reference not set to an instance of an object
NetWorkingServer.OnGUI () (at Assets/Scripts/General/Networking/NetWorkingServer.cs:40)
It is a basic lobby/hud setup i am trying and I am getting this error when I press the button to search for games/ListMatches. I should be getting the ‘no matches found messages’ instead i get this error which i am super confused about, probably something simple i am missing here. edit: the public objects have been set in the inspector so its not that.
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using UnityEngine.Networking.Match;
using UnityEngine.Networking.Types;
using System.Collections.Generic;
public class NetWorkingServer : MonoBehaviour{
public GameObject Menu;
public Text hostList;
public string gameName;
public NetworkManager networkManager;
//Usethisforinitialization
void Start(){
gameName = "game1";
Menu.SetActive (false);
networkManager = GetComponent<NetworkManager> ();
}
//Updateiscalledonceperframe
void Update(){
}
void OnGUI()
{
//gameplay related
//server related
if(GUI.Button(new Rect(100,100,250,100),"StartServer")){
HostServer(gameName);
}
if(GUI.Button(new Rect(100,250,250,100),"Find Game/Hosts")){
networkManager.matchMaker.ListMatches (0, 10, "game1", true, 0, 0, networkManager.OnMatchList);
if (networkManager.matches.Count !=0) {
Debug.Log ("A list of matches was found");
foreach (MatchInfoSnapshot match in networkManager.matches) {
hostList.text = match.name.ToString () + System.Environment.NewLine;
}
} else {
hostList.text = "No matches found";
Debug.Log ("No matches in requested room!");
}
}
}
public void HostServer(string matchName){
networkManager.StartMatchMaker ();
networkManager.matchMaker.CreateMatch (matchName, 4, true, "", "", "", 0, 0,networkManager.OnMatchCreate);
}