- Home /
How do I make a sound only play once?
Buddy is the quest before this script should execute. When the quest is over, it's supposed to play a 'subsound' after 5 seconds. However, this sonud just plays a million times.
pragma strict
var brunoSound : AudioClip;
public var pissed;
function Update () {
//DO THIS IF YELLING AT BUDDY IS TRUE
if (Buddy == true); WaitForSeconds(5); { audio.PlayOneShot(brunoSound); }
var Buddy = false;
//SUBSOUND
if (Buddy == false); var pissed = true;
}
Answer by Landern · Dec 30, 2014 at 08:18 AM
You're killing your if statement by ending it with a semi-colon. also, the order in which you're doing things is incredibly wrong, on line: 5 of your poorly formatted code, you're saying, if Buddy is true - END STATEMENT - then it yields for 5 seconds, then it plays audio, etc etc. You're also doing it below on line 8. You're also redefining variables(like pissed) in the update function. Buddy isn't defined whatsoever out of the function but between if statements, this code would explode.
Have a modified version that will probably still not work. Please watch the Unity learning/tutorial videos.
var Buddy = false;
var brunoSound : AudioClip;
public var pissed;
function Update () {
if (Buddy == true)
{
WaitForSeconds(5);
audio.PlayOneShot(brunoSound);
}
if (Buddy == false) {
pissed = true;
}
}
I love to go by trial and error ^^ Thanks for the help though! :)
Your answer
Follow this Question
Related Questions
How do I rotate my Camera around my sphere using the arrow keys? 0 Answers
help to add more details to my project 1 Answer
Input.GetKeyDown working smoothly 0 Answers
Unity freezes when I play or run 1 Answer
Prefab with a fixed position 0 Answers