- Home /
Play Sound on Score
I am trying to play a sound when a score is reached and I can't get anywhere.
var Score : int =0;
var ScoreSound : AudioClip;
function Update () {
guiText.text = "Score "+Score;
if(Score >= 1000)
Boo();
}
function Boo(){
audio.clip = ScoreSound;
audio.PlayOneShot(ScoreSound);
yield WaitForSeconds (audio.clip.length);
yield WaitForSeconds (5);
Application.LoadLevel ("WinMenu");
}
I think the problem is with if(Score >= 1000)
- I tried == but no luck. The sound will keep playing because the score is greater or equal than 1000.
Answer by Kourosh · May 02, 2011 at 02:07 PM
in Boo() function add this line:
Score =0;
it'll immediately stop the if statement from calling the function every frame.
It works sure enough but it will reset my score. I have other sounds playing at smaller score values before loading the level when reaching a high score.
$$anonymous$$ Poirot answer should be fine. Also if you can have 2 variables storing score value one to show on your guiText and one for reseting after conditions applied (the boo sound played).
Answer by M Poirot · May 02, 2011 at 02:43 PM
you should add an if statement which is true when the sound hasn't been played and which truns false when the sound is played.
var Score : int =0; var ScoreSound : AudioClip;
function Update () { guiText.text = "Score "+Score; if(Score >= 1000) Boo(); }
var playSound = true;
function Boo(){ if(playSound==true); { audio.clip = ScoreSound; audio.PlayOneShot(ScoreSound); yield WaitForSeconds (audio.clip.length); yield WaitForSeconds (5); Application.LoadLevel ("WinMenu"); playSound = false; } }
I haven't tested it but I thought it would world
I get this though from what it looks it should work: Assets/PiratesHat!/PiratesHatAssets/Scripts/TestScore.js(43,15): BCE0044: expecting :, found '='.
theres an extra ";" after (playSound==true) which should be removed.
It works awesomely; however, I don't understand how the >= works; Once I play the sound once (score >= 1000) I cannot repeat it at 2000, 3000 or any other higher score.
When your score turns 2000 the playSound var can be turned on again, which will get you the script which im posting below= means if your score equals or is bigger than thousand the sound should be played.
Your answer
![](https://koobas.hobune.stream/wayback/20220613130902im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Score system in C# [fixed] 1 Answer
Image popup when score reaches 10 C# 1 Answer
No sound in my video after capturing my screen? 0 Answers
Jumping sound not working after respawning 1 Answer
Get Audio from Speakers 0 Answers