- Home /
Random.range selecting multiple options
I am using the funcion Random.range to play randoms sounds for footsteps. The problem cosists of the fact that this part is selecting more that 1 sound:
waterFootseptsSounds[Random.Range(0,waterFootseptsSounds.Length)]
I tried to monitor the functionality of Random.Range and the result is that in just 1 sequence isolated by 2 yields I got 4 random numbers. So, the output is formed of 4 sounds started simultaneously.
This is the code :
IEnumerator FootstepsSounds() {
//Play sounds
if(simplyMoving){
if(onLand){
temporaryAudio=landFootstepsSounds[Random.Range(0,landFootstepsSounds.Length)];
yield return new WaitForSeconds(average);
audio.clip=temporaryAudio;
}
else if(inWater){
temporaryAudio=waterFootseptsSounds[Random.Range(0,waterFootseptsSounds.Length)];
yield return new WaitForSeconds(average);
audio.clip=temporaryAudio;
}
else{
temporaryAudio=woodFootstepsSounds[Random.Range(0,woodFootstepsSounds.Length)];
yield return new WaitForSeconds(average);
audio.clip=temporaryAudio;
}
audio.Play();
}
}
In addition I can tell that I start the couroutine in the Update function. It is started by the condition if(simplyMoving)
I don't work a lot with the audio classes and variables but I think it's because each time you hit a gameObject (part where you change the value of the current playing sound) you change the current playing song.
The play function don't have parameter, it just read the current song so you need to put a security to add a new song to play when the current one is over.
if you have this : audio.isPlaying to true value so don't do your Random.Range
if( !audio.isplaying ) playingsounds = ...
Your answer
Follow this Question
Related Questions
Last item not being moved (yield problem) 0 Answers
Coroutoutine doesnt work properly. 3 Answers
Getting an error when trying to yield 2 Answers