- Home /
Play animation and sound when GUI button is pressed
I have a GUI button that I have created. When the button is pressed I would like a game object to play a certain animation and also play a sound. I created the animation in Unity. Here is the script I have written for the GUI button.
static var triggerGo: int;
function OnGUI () { //Make a background box GUI.Box (Rect (500,120,100,90), "");
//Make the first button
if (GUI.Button (Rect(510,150,80,20), "Lever")){
}
}
The animation clip I created using keyframe animation in Unity is called lever1 and the soundclip is called lever.wav
I am still new to writing script. Can someone please show me how and where to place the animation and sound into the script from above? Thanks
$$anonymous$$ay I suggest you select an answer, if you think the answer you got is the correct one. This question just got bumped by the magical monkeys that operate everything around here because you have not selected an answer
Answer by joedrigon · Sep 02, 2010 at 06:21 PM
var lever : AudioClip;
if (GUI.Button (Rect(510,150,80,20), "Lever")) { animation.Play ("lever1"); audio.PlayOneShot(lever); }
Read this because I'm not sure what I have posted is 100% correct...
http://unity3d.com/support/documentation/ScriptReference/Animation.Play.html http://unity3d.com/support/documentation/ScriptReference/AudioSource.PlayOneShot.html