Hey Guys,
So I’m busy with creating a Multiplayer Towerdefence game and I’m currently setting up the menu with the NetworkManager script included so you can connect to a match and create a server.
Currently I’m running into an issue where I seriously can not find a solution for… hopefully you guys can help me out!
The error:
NullReferenceException: Object reference not set to an instance of an object
menu.Host () (at Assets/Scripts/menu.cs:46)
menu.OnGUI () (at Assets/Scripts/menu.cs:29)
The Menu Script :
using UnityEngine;
using System.Collections;
public class menu : MonoBehaviour {
private string CurMenu;
public string Name;
public string MatchName;
public int Players;
// Use this for initialization
void Start () {
CurMenu = "Main";
Name = PlayerPrefs.GetString("PlayerName");
}
// Update is called once per frame
void Update () {
}
void ToMenu (string menu) {
CurMenu = menu;
}
void OnGUI(){
if(CurMenu == "Main")
Main();
if(CurMenu == "Host")
Host();
if(CurMenu == "Lobby")
Lobby();
}
private void Main(){
if (GUI.Button(new Rect(0,0,128,32),"Host a match")){
ToMenu ("Host");
}
Name = GUI.TextField (new Rect (130, 0, 128, 32),Name);
if (GUI.Button (new Rect (260, 0, 128, 32),"Save")) {
PlayerPrefs.SetString ("PlayerName",Name);
}
}
private void Host(){
if(GUI.Button(new Rect (0, 0, 128, 32), "Start")){
NetworkManager.Instance.StartServer(MatchName,Players);
ToMenu("Lobby");
}
if(GUI.Button(new Rect (0, 33, 128, 32),"Back")) {
ToMenu ("Main");
}
MatchName = GUI.TextField(new Rect (130, 0, 128, 32),MatchName);
GUI.Label (new Rect (260, 0, 128, 32), "Match Name");
GUI.Label (new Rect (130, 33, 128, 32), "Max Players");
if(GUI.Button (new Rect (222, 33, 32, 32), "+"))
Players ++;
GUI.Label (new Rect (262, 33, 64, 32), Players.ToString());
if (GUI.Button (new Rect (282, 33, 32, 32), "-"))
Players --;
}
private void Lobby(){
if (GUI.Button (new Rect (Screen.width - 128, Screen.height - 64, 128, 32), "Start Game")) {
}
if (GUI.Button(new Rect(Screen.width - 128,Screen.height - 32,128,32),"Back")){
ToMenu ("Host"); }
}
private void MatchList()
{
if (GUI.Button (new Rect (0, 0, 128, 32), "Refresh"))
{
MasterServer.PollHostList();
}
if (GUI.Button (new Rect (0, 0, 128, 32), "Back"))
{
ToMenu ("Lobby");
}
GUILayout.BeginArea (new Rect (Screen.width / 2, 0, Screen.width / 2, Screen.height), "Server List");
foreach (HostData hd in MasterServer.PollHostList())
{
GUILayout.BeginHorizontal();
GUILayout.Label(hd.gameName);
if(GUILayout.Button("Connect"))
{
Network.Connect(hd);
ToMenu("Lobby");
}
GUILayout.EndHorizontal();
}
GUILayout.EndArea ();
}
}
The NetworkManager script:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class NetworkManager : MonoBehaviour {
public string PlayerName;
public string MatchName;
public static NetworkManager Instance;
public List<Player> Playerlist = new List<Player>();
// Use this for initialization
void Start () {
Instance = this;
DontDestroyOnLoad (gameObject);
}
// Update is called once per frame
void Update () {
}
public void StartServer(string ServerName, int MaxPlayers){
Network.InitializeSecurity();
Network.InitializeServer (MaxPlayers, 25565,true);
MasterServer.RegisterHost("Tut", ServerName, "");
Debug.Log("Started the server");
}
}
[System.Serializable]
public class Player{
public string PlayerName;
}
Can someone please help me out?
Thanks in advance!
- Jeroen Breevoort