- Home /
Play Animation on Button Press
Hello, I have a game I am making in which the character must press a button in order to make an animation play of a camera flash. I have discovered, though, it will not play the animation no matter what button I press. I have a feeling it's a problem with the Function, but I'm not sure. Here is the code:
#pragma strict
var flashSound : AudioClip;
var flashAnimation : Animation;
var flashAnimation2 :Animation;
var flashLight1 : GameObject;
var flashLight2 : GameObject;
function Start ()
{
if(Input.GetKeyDown("f"))
{
flashLight1.GetComponent.<Animation>().Play("Flash");
flashLight2.GetComponent.<Animation>().Play("Flash2");
GetComponent.<AudioSource>().clip = flashSound;
GetComponent.<AudioSource>().Play();
}
}
Thank you!
Answer by Hellium · Jun 24, 2015 at 10:47 AM
Change the function from Start() to Update().
Start is called only once when your object is ready (after calling Awake()), while Update is called every frame. In Update(), you will be able to detect if the user pressed the key with the Input.GetKeyDown function.
Thank you for your help! I tried Update() once before, but Unity popped up an error so I changed it to Start() ins$$anonymous$$d.