Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
Random.Range is exclusive for the upper-bound integer value
According to the unity docs: http://docs.unity3d.com/ScriptReference/Random.Range.html
For integers, the Random.Range is inclusive for the min and exclusive for the max, therefore the line of code to choose a random sound from the list of AudioClips:
AudioClip randomSound = soundsClips[Random.Range (0, soundsClips.Count - 1)];
should be just...
AudioClip randomSound = soundsClips[Random.Range (0, soundsClips.Count)];
Is this correct?
Nick PettitTreehouse Teacher
Wow, you're absolutely right! I thought Random.Range always used inclusive numbers, but that's probably because I usually use it with floats. When using integers, the max is indeed exclusive (probably to prevent the exact problem I thought I was solving by subtracting negative 1). I just tested this in the game by using some console debugging, and the last sound in the random sound player script never gets played even after ~1000 runs (or I'm extremely unlucky). When the "-1" is removed, all the sounds in the list are included in the selection.
This will require some time to fix, because it's a video fix. I'm hoping to get that done this week.