NullReferenceException on Singleton, that then works second time through

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

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?

Thanks to Twitter @DevinReimer on Twitter who suggested that perhaps OnSpawn() was being called before Start() - which was indeed the problem.

If OnSpawn() was called before Start(), strange that in the editor the Inspector was showing values set by Start().

This is related to execution order. This might help:

You can also set the execution order for your script in the Mono Manager: Edit->Project Settings->Script Execution Order