NullReferenceException Help please :>
Well, yeah. im following Gamer To Game Developers tutorial on making a game and im in chapter 2
Im getting this error: NullReferenceException: Object reference not set to an instance of an object Solution 'FPSSingleplayer' (2 projects) Assets/GAMES/Scripts/Masters/GameManager_TogglePauseMenu.cs 39
Heres my code, line, it points at the if() statement even though GTGD did the same thing as me, maybe its outdated or maybe i made a typo, it also gives me the same error for line 50, and 13.
using UnityEngine;
using System.Collections;
namespace Chapter2
{
public class GameManager_TogglePauseMenu : MonoBehaviour
{
private GameManagerMaster gameManagerMaster;
public GameObject menu;
void Start()
{
ToggleMenu();
}
void Update()
{
CheckForMenuToggleRequest();
}
void onEnable()
{
SetInitialReferences();
gameManagerMaster.GameOverEvent += ToggleMenu;
}
void onDisable()
{
gameManagerMaster.GameOverEvent -= ToggleMenu;
}
void SetInitialReferences()
{
gameManagerMaster = GetComponent();
}
void CheckForMenuToggleRequest()
{
if (Input.GetKeyUp(KeyCode.Escape)&& !gameManagerMaster.isGameOver && !gameManagerMaster.isInventoryUIOn)
{
ToggleMenu();
}
}
void ToggleMenu()
{
if (menu != null)
{
menu.SetActive(!menu.activeSelf);
gameManagerMaster.isMenuOn = !gameManagerMaster.isMenuOn;
gameManagerMaster.CallEventMenuToggle();
Debug.LogWarning("Wierd");
}
else
{
Debug.LogWarning("You didn't define the menu! Silly!");
}
}
}
}
Answer by Jessespike · Feb 02, 2016 at 09:43 PM
gameManagerMaster is null, retrace your steps to where it's supposed to get initialized. You'll find it's supposed to get initialized when the script gets enabled. But you misspelt the function name:
void onEnable()
should be
void OnEnable()
http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnEnable.html