Playing sounds on Animation

I would like to create a script that can be placed on the FPC, and allow me to play a sound if a specific animation is triggered. I had tried something like:

var tankButtons : AudioClip;
function Update () {
if (animation.Play(“tank_bttn_down”)) {
audio.PlayOneShot(tankButtons);
}
}

But this did not work. I have a lot of buttons and assets that use the same animation when triggered, and it would be ideal to play a sound based on that animation playing. Thank you in advance for any help you might provide.

I think u need somthing like this

function Update(){
//Input Name is the key you have to hit.
if( Input.GetButtonDown("InputName")){
animation.Play("tank_bttn_down");
audio.PlayOneShot(tankButtons);
Debug.Log ("This Button:Working");
}
}

UNTESTED-But i think this wil work. :smile:

In your original code the animation would need to be playing in order for you have the condition to be checked. You can insert the condition(what ever kind u want) in the if statment then play the animation and clip at the same time. Also the animations script does not have to be on the animated whatever. Just create a var for a gameobject or its transform and put that var before the animation.Play line.

I’m no pro, by helping you it helps me learn too. :smile: