null exception reference on animation Unity 3.5+

Hello everyone, it appears that whenever I interact with one of my gameobjects I keep getting this error message null exception reference. I’m pretty sure that it has to do with the animation espect (even though it does work when I select play on awake), but also unity keeps referencing it.

Anyway Here’s the code I attach to my first version controller, I’m just basically using a simple raycasting and if I were to press the letter E on the recorder then it access another script where an animation along with an sound is played.

#pragma strict

// Exit Door

var recorder : GameObject;
var Cube1 : GameObject;


function Update () {

	var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
	var hit : RaycastHit;

	if(Input.GetKeyDown("e")){
		if(Physics.Raycast (ray, hit, 2)) {
	
			Debug.Log("Name: "+hit.collider.name);
			
			if(hit.collider.name == "Recorder"){
				recorder.GetComponent (RecorderOn).RecoOn();
			
			}	
		}
	}
}

The other Script is here and its called RecorderOn:

var recorder : GameObject;
var Cube1 : GameObject;
var recording : AudioClip;

function RecoOn () {

	recorder.transform.parent.animation.Play("RecOn"); // This is the error
	audio.PlayOneShot(recording);
	gameObject.Find ("Cube1").renderer.enabled = true;

}

Once Again any help is highly appreciated and thanks!

2 Answers

2

Try this:

function RecoOn() {
     GameObject.Find("// Parent GameObject").animation.Play("RecOn");
     audio.PlayOneShot(recording);
     GameObject.Find ("Cube1").renderer.enabled = true;
}

Also, it’s ‘GameObject.Find’.

Hey thanks for the help, unfortunately it still gives me the error on line "GameObject.Find ("recorder").animation.Play("RecOn");"

Either recorder is null (“has not been given any value”) or that object doesn’t have an animation component attached.