NullReferenceException: Object reference not set to an instance of an object - MonoBehaviour

This is driving me mad as this script should work and makes sense to me, basically where the error is occurring is where I want a MonoBehaviour script to be disabled/false and the other one to be true, here is the first 26 lines of code.

#pragma strict

//Inspector variables

public var actionOnSpotted : MonoBehaviour;
public var soundOnSpotted : AudioClip;
public var actionOnLostSight : MonoBehaviour;

//Private variables

private var player : Transform;
private var enemy : Transform;
private var visualArea : boolean = true;

function Awake ()

{
	player = transform;
	enemy = GameObject.FindWithTag("Enemy").transform;
}

function OnEnable()

{
	actionOnLostSight.enabled = true;
	actionOnSpotted.enabled = false;
}

Now it is saying the error is on line 26 and saying that it’s not referencing anything, but on the Inspector there is a script attached so in theory, this code should be working fine…Or am I missing something?

if it serves script cs, then JavaScript compiles before and don't see it unless it is in a certain folder like plug-ins or editor folder or something

2 Answers

2

Try putting the “actionOnSpotted” script on Standart Assets folder so it would compile before other scripts.If that solves your problem that means it was about the compiling order.

More detailed information can be found here : http://docs.unity3d.com/Documentation/ScriptReference/index.Script_compilation_28Advanced29.html

Thanks, that sorted the problem!

Can u choose my answer as correct if it solved :) I need a bit karma :)