I’m getting a NullReferenceException when attempting to access either functions or variables on a MonoBehaviour singleton. Yet I can see in the inspector that the singleton has been assigned to the variable through which I’m accessing the singleton.
GameManager is attached to only one object in the scene
class GameManager extends MonoBehaviour
{
public static var instance : GameManager;
public var test : int = 1234;
function Awake() { instance = this; }
function GetValue() : boolean
{
return Random.value;
}
}
My Caveman script, which fails when attempting to use the Game Manager.
class CavemanEntering extends MonoBehaviour
{
private var gm : GameManager;
function Start()
{
gm = GameManager.instance;
}
function OnSpawn() // gets called several seconds after starting the level
{
// the line below returns a Null Reference Exception...
// ...yet in the Inspector gm is clearly assigned
gm.GetValue();
}
}
Running this and Unity stops at “gm.GetValue()” with the NRE. Resuming play, however, and the next time OnSpawn is called it runs through successfully.
Thoughts? What am I missing?