audio keeps playing despite oneShot
I have created a script (that has some more in it) but i have displayed the important parts that are involved with the problem. The script allows a block to display a different colour depending on what random number it's given. For some reason when i select the good blocks they don't play the audio clip just once it constantly repeats despite the use of oneShot.
var backspin : boolean;
public var GoodSound : AudioClip;
function Update () {
if(backspin == true){
retexture -= Time.deltaTime;
if(retexture < 0){
if(number < 4){
Bad ();
}
if(number >= 4){
Good ();
audio.PlayOneShot (GoodSound);
}
}
}
}
function OnMouseDown () {
backspin = true;
}
can anyone help? i can put the whole script in if needs be, just didn't want too put in too much.
I believe PlayOneShot only makes it so a new call won't restart the sound. You would have to do some more if you only want it to play once. For example you could create a hasPlayed boolean that you set to true when starting the sound, and reset it to false somewhere else (For example on mouse up) Then you could do if(backspin == true && hasPlayed == false). backspin could potentially be used for the same purpose, i see no place where you set it to false so the if check in update would always be true after the first click. $$anonymous$$aybe set it to false when starting the sound?
Answer by aditya · Apr 19, 2016 at 12:07 PM
in Update
after checking your backSpin
for true
simply set it back to false
function Update () {
if(backspin == true){
backspin = false;
retexture -= Time.deltaTime;
if(retexture < 0){
if(number < 4){
Bad ();
}
if(number >= 4){
Good ();
audio.PlayOneShot (GoodSound);
}
}
}
}