Hi!Can you please fix my code?My problem is when you click on text(with box collider) audio sonds,but if you click it 2x or more fast same audio sounds many times.I want to make,when you press one of the text(which plays audio)you can’t press it again when it sounds or other audio.
I need to make that when audio sounds,you can’t turn on other audios until 1st stops to sound.And if audio is pressed and you press it again,it stops.
#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;
}
isPlaying doesn’t work when used with PlayOneShot because isPlaying checks the current clip and PlayOneShot does not assign the clip property. I would just assign the clip and use Play which would allow isPlaying to work.
I just wan’t to make my code not to allow play many sounds at same moment,i wan’t to make that only one audio sounds at the time and if you click same audio again,it stops.Sorry for my bad english,
Es nevēlos,lai vairāki audio skan vienlaikus.Gribu,lai kods tikai atļauj vienam audio skanēt uz momentu un pārējos var tikai slēgt kad tas pirmais audio ir noskanējis.Vai arī,kad uzspied uz tā audio 2x tas beidz skanēt.
Something like this would work. Just assign whatever sound you want to play with audio.clip = whateverVariable; before using audio.Play();
Also use audio.Stop(); to stop the current audio before assigning a different clip to it, that way you stop the current sound playing before playing the new one, as well.
Not related, but just a tip. You’ll likely get more responses if you use the code tags when posting your scripts on the forums. =)
#pragma strict
var isQuitButton = false;
var sound : AudioClip;
function OnMouseDown()
{
if(!audio.isPlaying)
{
renderer.material.color = Color.red;
audio.clip = sound;
audio.Play();
waitForAudio(audio.clip.length);
}
else
{
audio.Stop();
}
}
function waitForAudio(time:float)
{
yield WaitForSeconds(time);
//change color back
renderer.material.color = Color.white;
}