- Home /
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!
Answer by Loius · Nov 17, 2012 at 08:41 PM
Either recorder is null ("has not been given any value") or that object doesn't have an animation component attached.
Answer by Coreyf716 · Nov 17, 2012 at 07:24 PM
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");"
Your answer
Follow this Question
Related Questions
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
Scripting error #2! 2 Answers
I can't script enemy animation 0 Answers