- Home /
How do I play a random sound each 5 seconds?
I now have this code:
var sound1 : AudioClip;
var sound2 : AudioClip;
var sound3 : AudioClip;
var sound4 : AudioClip;
var sound5 : AudioClip;
var playSound : int;
function Start () {
playSound = Random.Range(1, 5);
while(playSound>0){
if(playSound == 1){
sound1.audio.Play();
yield WaitForSeconds(5);
}
else if(playSound == 2){
sound2.audio.Play();
yield WaitForSeconds(5);
}
else if(playSound == 3){
sound3.audio.Play();
yield WaitForSeconds(5);
}
else if(playSound == 4){
sound4.audio.Play();
yield WaitForSeconds(5);
}
else if(playSound == 5){
sound5.audio.Play();
yield WaitForSeconds(5);
}
}
}
But I get an error on every line with audio.Play(); with text about Boo when I'm writing in JS. Also no sound comes.
As a side note, avoid while loop as you are doing in a Start function.
playSound = Random.Range(1, 5);
while(playSound>0)
playSound will always (as in forever) be greater than 0 since it is between 1 and 5. As a result, the script will never (as in always) leave the start function.
As a rule of thumb, do not use while loop of this kind in Update or Start (or any function actually). You cannot control how long it will take for the sentinel to be false and leave the loop.
You may ins$$anonymous$$d start a coroutine which will run until the condition is met without blocking the rest of the game.
You could check this http://unitygems.com/coroutines/ for more info on coroutine and their usage.
Alright, thank you. I have used while loop in other scripts since I thought it was the easiest way to have something go on forever but I will keep that in $$anonymous$$d!
It is fine as long as you control how many round it will be performed. Example:
var gos :GameObject[] =GameObject.FindObjectsWithTag("Enemy");
while(gos.$$anonymous$$oveNext()){
//do Stuffs
}
$$anonymous$$oveNext will return false when getting out of range and gos is of limited amount so we are sure to get to the end at some point. This one is fine in a function.
But in your way, random will never return and even though you would have:
var num = Random.Range(0,5);
while(num>0)
You cannot know how many round the compiler will take before returning 0, maybe it will take 10s. (let's not get into probability shall we)
Answer by fafase · Mar 09, 2013 at 05:21 PM
var sound : AudioClip[] = new AudioClip[5];
function Start(){
InvokeRepeating("PlayClipAndChange",0.01f,5.0f);
}
function PlayClipAndChange(){
audio.clip = sound[Random.Range(0, 5)];
audio.Play();
}
To be tried but that should do it. You have an array in which you drag and drop your audio clips. Then you start InvokeRepeating which calls the function each 5 sec. Then, in the function, the sound is assigned using a random value and played.
Your answer is righter. I didn't even notice that the whole thing was in Start()- of course it's not going to play every 5 seconds without an InvokeRepeating(). Going to go pour myself a new cup of coffee.
Thank you, that worked! Although I must ask, why use [Random.Range(0, 6)] since it only contains 5 clips? I changed it to 4 ins$$anonymous$$d of 6 and it worked but is there anything I may have done wrong by that?
Actually you need to use sound[Random.Range(0,5)], because arrays start at 0, (0,6) will sometimes return sound[5] which is actually the nonexistant 6th sound and throw an IndexOutOfRangeException.
But Random.Range to get an int will never return the top number. So you have to put it one more than one less than the real number you have to choose from, which is... 5. $$anonymous$$akes perfect sense, right?
Oops yep right, the array is 5 so the max value is 4 then Range is (0,5) This is because when using int max value is excluded, if using float max value is included. Don't ask why...
Alright then I get it, I knew about that it started at 0 but didn't know it didn't go to the highest.
Edit: And also to answer fafase above I have used the exact same style which I had here, so it was impossible for it to ever stop.
Your answer
Follow this Question
Related Questions
Random Audioclip. No Repeat ?? 1 Answer
Play a short audio file, then random delay, then repeat. 1 Answer
How to Delay A Ball shooting script 1 Answer
How do you delay Application.LoadLevel ? 2 Answers
Random footsteps 5 Answers