- Home /
Help! NullReferenceException?
Hi I'm new, but anyways, every time I start the game it gives me this: NullReferenceException: Object reference not set to an instance of an object menumanager.menu_main () (at Assets/menumanager.cs:41) menumanager.OnGUI () (at Assets/menumanager.cs:18)
menumanager.cs script:
using UnityEngine;
using System.Collections;
public class menumanager : MonoBehaviour {
public string CurrentMenu;
public string MatchName;
public string MatchPassword;
public int MatchMaxPlayers = 32;
void start()
{
CurrentMenu = "Main";
}
void OnGUI()
{
if (CurrentMenu == "Main")
menu_main();
if (CurrentMenu == "Lobby")
Menu_Lobby();
if (CurrentMenu == "Host")
Menu_HostGame();
}
public void NavigateTo(string nextmenu)
{
CurrentMenu = nextmenu;
}
private void menu_main()
{
if (GUI.Button(new Rect(10, 10, 200, 50), "Host Game"))
{
NavigateTo("Host");
}
GUI.Label(new Rect(220, 10, 130, 30), "Player Name");
Mplayer.instance.PlayerName = GUI.TextField(new Rect(400, 10, 200, 30), Mplayer.instance.PlayerName);
}
private void Menu_HostGame()
{
if (GUI.Button(new Rect(10, 10, 200, 50), "Back"))
{
NavigateTo("Main");
}
if (GUI.Button(new Rect(10, 60, 200, 50), "Start Server"))
{
Mplayer.instance.StartServer(MatchName, MatchPassword, MatchMaxPlayers);
}
GUI.Label(new Rect(220, 10, 130, 30), "Match Name");
MatchName = GUI.TextField(new Rect(400, 10, 200, 30), MatchName);
GUI.Label(new Rect(220, 50, 130, 30), "Match Password");
MatchPassword = GUI.TextField(new Rect(400, 50, 200, 30), MatchPassword);
GUI.Label(new Rect(220, 90, 130, 30), "Max Players");
GUI.Label(new Rect(400, 90, 200, 30), MatchMaxPlayers.ToString());
MatchMaxPlayers = Mathf.Clamp(MatchMaxPlayers, 8, 32);
if (GUI.Button(new Rect(425, 90, 25, 30), "+"))
MatchMaxPlayers += 1;
if (GUI.Button(new Rect(450, 90, 25, 30), "-"))
MatchMaxPlayers -= 1;
}
private void Menu_Lobby()
{
}
}
my Mplayer.cs script:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Mplayer : MonoBehaviour {
public static Mplayer instance;
public string PlayerName;
private string MatchName = "";
private string MatchPassword = "";
private int MatchMaxUsers = 32;
public List<MPPlayer> PlayerList = new List<MPPlayer>();
void start()
{
instance = this;
}
public void StartServer(string servername, string serverpassword, int maxusers)
{
MatchName = servername;
MatchPassword = serverpassword;
MatchMaxUsers = maxusers;
Network.InitializeServer(MatchMaxUsers, 2550, false);
MasterServer.RegisterHost("Free For All", MatchName, "");
Network.InitializeSecurity();
}
void OnServerInitialized()
{
Server_PlayerJoinRequest("", Network.player);
}
void OnConnectedToServer()
{
networkView.RPC("Server_PlayerJoinRequest", RPCMode.Server, "", Network.player);
}
void OnPlayerDisconnected(NetworkPlayer id)
{
networkView.RPC("Client_RemovePlayer", RPCMode.All, id);
}
[RPC]
void Client_RemovePlayer(NetworkPlayer view)
{
MPPlayer temppl = null;
foreach(MPPlayer pl in PlayerList)
{
if(pl.PlayerNetwork == view)
{
temppl = pl;
}
}
if (temppl != null)
{
PlayerList.Remove(temppl);
}
}
[RPC]
void Server_PlayerJoinRequest(string playername, NetworkPlayer view)
{
networkView.RPC("Client_AddPlayerToList", RPCMode.All, playername, view);
}
[RPC]
void Client_AddPlayerToList(string playername, NetworkPlayer view)
{
MPPlayer tempplayer = new MPPlayer();
tempplayer.PlayerName = playername;
tempplayer.PlayerNetwork = view;
PlayerList.Add(tempplayer);
}
public class MPPlayer
{
public string PlayerName = "";
public NetworkPlayer PlayerNetwork;
}
}
So if someone could help, thanks.
Answer by jreitman · Dec 14, 2012 at 06:25 AM
I am guessing you need to fix your script execution order
Mplayer.instance is null probably because it is not created before your other object.
Fix the script execution order or have Mplayer's start() call actually be an awake() call.
Answer by skalev · Dec 14, 2012 at 02:21 AM
You didn't add the line numbers, but you have the solution right in the error. Something called at line 18 or 41 of Menu Manager is null when you try and access it. Look at your code, and find what is the null reference. Putting a break point can do wonders in finding out the problem as well.
Answer by Landern · Dec 14, 2012 at 02:33 AM
When you instantiate players name, it is null. When you declare it, give it a default value instead of null(i.e. empty string), you are using it in the GUI.Textfield when it is null, your singleton looks good.
public string PlayerName = string.Empty;
This is for the class Mplayer not MPPlayer which you did give an initial value.
Your answer
