Object reference not set without object? Calling ListMatches and getting this error
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);
}
Answer by nasir_41 · Feb 02, 2017 at 06:19 AM
After Calling this Methode
networkManager.matchMaker.ListMatches (0, 10, "game1", true, 0, 0, networkManager.OnMatchList);
You probably need to wait for the callback here. And put the following code.. In the callback method.
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!"); } }
Answer by newbienoob · Feb 02, 2017 at 07:06 AM
@nasir_41 how would i go about waiting for the callback? i was under the assumption that NetworkManager.OnMatchList would automatically do that.
Your answer
![](https://koobas.hobune.stream/wayback/20220612110138im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unet local matchmaking 0 Answers
Unet- Null refernence OnMatchCreate , only on Iphone build 0 Answers
netcode throws hash errors when attempting to spawn local player 0 Answers
How to implement steam's matchmaking service with unity networking? 2 Answers
I'm getting an error: "AudioClip can't be deserialized because it has no default constructor" 1 Answer