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

I’m sure that this has been answered somewhere else but I haven’t found it yet and I sort of need to turn in this project for class tonight.

I am using Will Goldstone’s “Unity Game Development Essentials” for a class and I am having a weird error creep up. (See title)

Below is my code:

	private var doorIsOpen : boolean = false;
	private var doorTimer : float = 0.0;
	private var currentDoor : GameObject;

	var batteryCollect : AudioClip;
	var doorOpenTime : float = 3.0;
	var doorOpenSound : AudioClip;
	var doorShutSound : AudioClip;
	
function Update () {

	var hit : RaycastHit;
	
		if(Physics.Raycast (transform.position, transform.forward, hit, 5)) {
		
		if(hit.collider.gameObject.tag=="outpostDoor" && doorIsOpen == false && BatteryCollect.charge >= 4){
			GameObject.Find("Battery GUI").GetComponent(GUITexture).enabled=false;
			currentDoor = hit.collider.gameObject;
			Door(doorOpenSound, true, "dooropen", currentDoor);
				
			}
		}
		
		else if(hit.collider.gameObject.tag=="outpostDoor" && doorIsOpen == false && BatteryCollect.charge < 4){
			GameObject.Find("Battery GUI").GetComponent(GUITexture).enabled=true;
			TextHints.message = "The door seems to need more power...";
			TextHints.textOn = true;
		}
	
	if(doorIsOpen){
		doorTimer += Time.deltaTime;
		
		if(doorTimer > 3){
			Door(doorShutSound, false, "doorshut", currentDoor);
			doorTimer = 0.0;
		}
	}
}
/*
function OnControllerColliderHit(hit : ControllerColliderHit) {
	if(hit.gameObject.tag == "outpostDoor" && doorIsOpen == false){
		currentDoor = hit.gameObject;
		Door(doorOpenSound, true, "dooropen", currentDoor);
	}
}
*/

function Door(aClip : AudioClip, openCheck : boolean, animName : String, thisDoor : GameObject){
	audio.PlayOneShot(aClip);
	doorIsOpen = openCheck;
	
	thisDoor.transform.parent.animation.Play(animName);
	
}

function OnTriggerEnter(collisionInfo : Collider){

	if(collisionInfo.gameObject.tag == "battery"){
		BatteryCollect.charge++;
		audio.PlayOneShot(batteryCollect);
		Destroy(collisionInfo.gameObject);
	}
}

@script RequireComponent(AudioSource)

For some reason I am getting this error:

NullReferenceException: Object reference not set to an instance of an object
PlayerCollisions.Update () (at Assets/Scripts/PlayerCollisions.js:24)

But I am very confused because line 24, which is:

	else if(hit.collider.gameObject.tag=="outpostDoor" && doorIsOpen == false && BatteryCollect.charge < 4){

Will not work at all if “else” is there, but once I remove it, the error continues BUT it works like its suppose to.

I’m very confused, so if anyone would be able to look at this, I would greatly appreciate it.

Thanks!

I hate it when code isn’t tested before it’s published, unless it has such a disclaimer on it.

It should be ‘batteryCollect’ with a small b, because that’s how the variable is declared above. That, or the hit doesn’t have a collider, or the collider doesn’t have a game object.

Why not run this in the debugger, where it will stop on this line, and you can inspect the variables and see what isn’t existing?
If you don’t want to mess with the debugger, you can use Debug.Log to ‘dump’ the variables (before this problem line), to see what values. One of them will be null.

But it’s probably that ‘b’ thing.