- Home /
Question by
Mr.BMS · Dec 26, 2014 at 03:53 PM ·
javascriptaudiobugsounds
Audio Repeating.
Hello,can you please fix my audio bug.I made code,when you click on text with box collider,audio sounds,but when i click it 2x fast same audio plays many times at same moment.Can you please fix it?I need to make,when you click text again,audio stops on second click.First click turns on sound,second turns off.
#pragma strict
var isQuitButton = false;
var sound : AudioClip;
function OnMouseDown()
{
renderer.material.color = Color.red;
audio.PlayOneShot(sound);
waitForAudio(audio.clip.length);
}
function waitForAudio( time:float){
yield WaitForSeconds(time);
//change color back
renderer.material.color = Color.white;
}
Comment
Answer by Landern · Dec 26, 2014 at 03:53 PM
Check the isPlaying variable, if it's playing, don't play it again, if not, give it a play and invoke the coroutine.
function OnMouseDown()
{
// if audio is not playing, play it.
if (audio.isPlaying == false) {
renderer.material.color = Color.red;
audio.PlayOneShot(sound);
waitForAudio(audio.clip.length);
} else {
audio.Stop();
}
}
function waitForAudio( time:float){
yield WaitForSeconds(time);
//change color back
renderer.material.color = Color.white;
}
Your answer
Follow this Question
Related Questions
Why do sounds cancel out other sounds? 2 Answers
Why are these errors messing with me? 2 Answers
Audio loop after time 2 Answers
AssetPostprocessor - Can't change Mesh Data 2 Answers
Sound playing at random. (JS) 2 Answers