I keep getting a NullReferenceException whenever I am trying to disable a script from another script. The exact error is NullReferenceException: Object Reference not set to an instance of an object. I really don’t understand what is going on and it’s really getting on my nerves, any help would be appreciated. Code is below.

var information: String;

var script: MouseLookPlus;

var script2: MouseLookPlus;

private var guiOn = false;

private var rect: Rect;

function MouseToggle () {
	script = GetComponent(MouseLookPlus);
	script.Enabled = false;
	script2 = GetComponent(MouseLookPlus);
	script2.Enabled = false;


function OnMouseDown () {
	guiOn = true;
	rect = Rect(Screen.width/2-120,Screen.height/2-100,300,300);

function OnGUI () {

	if (guiOn){
		GUI.Label(rect, information);
		if (GUI.Button(Rect(Screen.width/2-0,Screen.height/2-50,75,50), "False")) {
				guiOn = false;
		if (GUI.Button(Rect(Screen.width/2-100,Screen.height/2-50,75,50), "True")) {
			animation.Play ("dooropen1");
			guiOn = false;

I have two Script variables because there are two scripts I’m attempting to disable when the GUI comes on.

Oh yeah, robertbu got it right. GetComponent will only work like that if MouseLookPlus is attached to the same object as this script.

You need to do something like:

script = GameObject.Find("Main Camera").GetComponent(MouseLookPlus);

…basically get hold of your camera object and call GetComponent on that.