Can't see what is wrong in my script here...:(

I’m calling a simple animation in my script, and when i press “R” in game, an error appears:
"MissingMethodException: UnityEngine.AnimationClip.Play
Boo.Lang.Runtime.DynamicDispatching.MethodDispatcherFactory.ProduceExtensionDispatcher ()"

PS: *This error reffers to line 22 of my script
I’m just starting with animations in scripts, but i can’t see what i have done wrong in here, Any ideas please??:slight_smile:
The Script:

public var DigitalCameraAnim : AnimationClip; 
public var DigitalCameraScreen : GameObject;
public var DigitalCameraLights : GameObject;

public var DigAnimation : AnimationClip;
var shootSound:AudioClip;


function Start(){

DigitalCameraScreen.active = false;


}

function Update () {

    if (Input.GetKeyDown(KeyCode.R)) ToggleDigitalCamera();
}        
function ToggleDigitalCamera () {

DigitalCameraAnim.Play("DigitalCameraAnimation");
audio.PlayOneShot(shootSound);

yield WaitForSeconds(0.6);

DigitalCameraScreen.active = true;
DigitalCameraLights.active = true;


}

You’re trying calling the the function ‘Play’ on an AnimationClip, which has no such function. What you want to do is call the function on the Animation component.

animation.Play ("DigitalCameraAnimation");

In the future, if you place “#pragma strict” at the top of your JavaScript files you will get this error during compile time rather than at runtime.

Weird, now i have a new error, and i don’t get it, because it says i don’t have the animation on the Player, but i do have it…, Look here is the error:

"MissingComponentException: There is no ‘Animation’ attached to the “Player” game object, but a script is trying to access it.
You probably need to add a Animation to the game object “Player”. Or your script needs to check if the component is attached before using it."

This is how the inspector looks with the script:

1407334--73325--$InspectorCamera.jpg
And here is the location of the animation:

Is there a way to reffer to this animation in the script, or do something in the script, to avoid changing the order or position of the animation/objects in the hierarchy?

Sorry here is the Script with your addition DanielQuick:

public var DigitalCameraAnim : AnimationClip; 
public var DigitalCameraScreen : GameObject;
public var DigitalCameraLights : GameObject;


var shootSound:AudioClip;


function Start(){

DigitalCameraScreen.active = false;


}

function Update () {

    if (Input.GetKeyDown(KeyCode.R)) ToggleDigitalCamera();
}        
function ToggleDigitalCamera () {

animation.Play ("DigitalCameraAnimation");
audio.PlayOneShot(shootSound);

yield WaitForSeconds(0.6);

DigitalCameraScreen.active = true;
DigitalCameraLights.active = true;

}

An Animation component and an AnimationClip are two different things. You are missing the Animation component (Add Component → Animation)

Ok working now, thanks a lot DanielQuick:). Do you mind if i post here another question about this same script in a while?, I’m trying to make the camera on/off with the same key “R”

This is what i have, but it’s not working, i’m trying to make that when i press “R” the animation of the camera starts, then i have to wait until it’s over(can’t press “R” while it’s playing), and when it’s over, if i press “R” again, the camera turn off this time(lights, and camera object=false). I would like if you could point me where i’m doing it wrong, in my opinion it’s fine, but it display the classic errors:
Assets/DigitalCameraScript.js(42,10): BCE0044: expecting (, found ‘ToggleDigitalCameraOff’.
And:
Assets/DigitalCameraScript.js(42,35): BCE0044: expecting :, found ‘{’.

I did a boolean variable, to check the state of the camera, so unity knows when it’s Off and On, but i still don’t know whats wrong…Please Help:)

The Script Right Now:

public var DigitalCameraAnim : AnimationClip; 
public var DigitalCameraScreen : GameObject;
public var DigitalCameraLights : GameObject;

private var ReadyAgain : boolean = true;

var shootSound:AudioClip;


function Start(){

DigitalCameraScreen.active = false;
ReadyAgain = true;

}

function Update () {

    if(ReadyAgain)
    if (Input.GetKeyDown(KeyCode.R)) ToggleDigitalCamera();
    
else ToggleDigitalCameraOff(); 


}        
function ToggleDigitalCamera () {

animation.Play ("DigitalCameraAnimation");
audio.PlayOneShot(shootSound);

yield WaitForSeconds(0.6);

DigitalCameraScreen.active = true;
DigitalCameraLights.active = true;
ReadyAgain = false;

//
yield WaitForSeconds(1);

{
function ToggleDigitalCameraOff() {

DigitalCameraScreen.active = false;
DigitalCameraLights.active = false;
}
}
//
}

You need to have braces for your if-else statement in Update if it goes over 1 line

 if(ReadyAgain)
{
    if (Input.GetKeyDown(KeyCode.R)) 
        ToggleDigitalCamera();
}

Also, it seems you placed extra braces at line 40 and 46.

Thanks for teaching me that:), it still have 3 errors though, i think they are of the “extra” or “missing” braces kind, can’t really tell what is wrong, do you see where is the mistake??, here are the errors:

  1. Assets/DigitalCameraScript.js(43,10): BCE0044: expecting (, found ‘ToggleDigitalCameraOff’.
  2. Assets/DigitalCameraScript.js(43,34): UCE0001: ‘;’ expected. Insert a semicolon at the end.
    3 Assets/DigitalCameraScript.js(45,28 ): BCE0044: expecting :, found ‘=’.

And the script Updated;):

public var DigitalCameraAnim : AnimationClip; 
public var DigitalCameraScreen : GameObject;
public var DigitalCameraLights : GameObject;

private var ReadyAgain : boolean = true;

var shootSound:AudioClip;


function Start(){

DigitalCameraScreen.active = false;
ReadyAgain = true;

}

function Update () {

    if(ReadyAgain)
    {
    if (Input.GetKeyDown(KeyCode.R)) ToggleDigitalCamera();
    }            
else ToggleDigitalCameraOff(); 


}        
function ToggleDigitalCamera () {

animation.Play ("DigitalCameraAnimation");
audio.PlayOneShot(shootSound);

yield WaitForSeconds(0.6);

DigitalCameraScreen.active = true;
DigitalCameraLights.active = true;
ReadyAgain = false;


yield WaitForSeconds(1);


function ToggleDigitalCameraOff() {

DigitalCameraScreen.active = false;
DigitalCameraLights.active = false;
}
}

You placed your ToggleDigitalCameraOff function inside your ToggleDigitalCamera function.

THANKS!!!, Finally got it working:), it has been a long time for this script, one last thing, just if you know about this. I added two lines to play the animation backwards (speed = -1), and it show no errors, so i think i’m doing it right, but when i play my game, i press “R”, and the normal animation plays, but when pressing “R” again, the backwards animation doesn’t play, and i can’t play the first one either(Everything else works, except for the animations from this moment). I you know what could be wrong, please let me know, and if you don’t know, no problem, you have helped me a lot today, thanks a lot for teaching me this things(Y), i really apreciate your patience with me haha:smile:

The Script so Far:):

public var DigitalCameraAnim : AnimationClip; 
public var DigitalCameraScreen : GameObject;
public var DigitalCameraLights : GameObject;

private var ReadyAgain : boolean = true;

var shootSound:AudioClip;


function Start(){

DigitalCameraScreen.active = false;
ReadyAgain = true;

}

function Update () {

    if(ReadyAgain)
    {
    if (Input.GetKeyDown(KeyCode.R)) ToggleDigitalCamera();
    }            
else if (Input.GetKeyDown(KeyCode.R))ToggleDigitalCameraOff(); 


}        
function ToggleDigitalCamera () {

animation.Play ("DigitalCameraAnimation");
audio.PlayOneShot(shootSound);

yield WaitForSeconds(0.6);

DigitalCameraScreen.active = true;
DigitalCameraLights.active = true;
ReadyAgain = false;


yield WaitForSeconds(1);
}

function ToggleDigitalCameraOff() {

DigitalCameraScreen.active = false;
DigitalCameraLights.active = false;

//THIS TWO ARE NOT WORKING IN GAME, THEY "DISSABLE?" THE FURTHER ANIMATIONS...:/

//animation.Play ("DigitalCameraAnimation");
//animation["DigitalCameraAnimation"].speed = -1;


ReadyAgain = true;
}

You have to change the speed before you play the animation. If you think about it, you’re playing the animation then changing the speed.

Do you mean like this??, i’m getting the same result:/, no animation is played, and no animation can be played after(not even the first one…)
I writed in the script:

function ToggleDigitalCameraOff() {

DigitalCameraScreen.active = false;
DigitalCameraLights.active = false;

animation["DigitalCameraAnimation"].speed = -1;
animation.Play ("DigitalCameraAnimation");


ReadyAgain = true;
}