- Home /
Noob Question: Play Specific Audio
Hello, This is a real noob question.
But what I want, is to be able to apply an audio clip to a variable, and play it depending on a boolean:
var AudioClip : Audio //(?)\\
var Play : false
then if play is true:
if(Play == true){
play AudioClip;
}
I'm guessing I will need to add an AudioSource to the object too.
Answer by Statement · Dec 10, 2010 at 10:37 PM
Add an audio source to your object. Then you can play the sound in two ways.
You can use PlayOneShot if you have an audio clip.
var clip : AudioClip; var play = false;
if ( play ) { audio.PlayOneShot( clip ); }
Or you can set the clip directly on the Audio Source in the inspector call Play:
var play = false;
if ( play ) { audio.Play( ); }
However, beware that setting the play flag to true and not resetting it will cause your sound to play every time that piece of code is run. This can crash your editor (I think).
Your answer
Follow this Question
Related Questions
Audio Clip Plays 1 turn too late 1 Answer
Playing Sound 2 Answers
Unity 2d toolkit, play specific clip 1 Answer
Play Audio Sound OnTriggerEnter 2 Answers
play an audio clip from a GUI button 1 Answer