- Home /
Index out of range exception
I realise this is probably a pretty common question, but I've looked at other answers and I really have no idea why these errors are coming up.
I have dragged four sounds into inspector to set up the array, and when I play the scene the ladder sounds work perfectly fine, as in it randomly chooses one of the four sounds and plays them like it should.
The error:
IndexOutOfRangeException: Array index is out of range.
WalkSoundScript+$PlayLadderSound$160+$.MoveNext() (at Assets/scripts /WalkSoundScript.js:83)
The Script in question:
#pragma strict
var sounds : AudioClip[];
var ladderSounds : AudioClip[];
var sneakSpeed : float = 0.04;
var runSpeed : float = 0.06;
var sneakDelay : float = 0.8;
var runDelay : float = 0.35;
var sneakVolume : float = 0.5;
var runVolume : float = 1.0;
var ladderDelay : float = 0.8;
var ladderVolume : float = 0.2;
var isGuard : boolean = false;
private var lastPosition : Vector3 = Vector3.zero;
private var isPlaying : boolean = false;
private var isPlayingLadder : boolean = false;
private var isJumping : boolean = false;
private var speed : float = 0f;
function Start () {
}
function Update () {
if(speed > runSpeed){
PlaySound(runDelay, runVolume);
}
else if(speed > sneakSpeed){
PlaySound(sneakDelay, sneakVolume);
}
if(PlayerScript.onLadder && Mathf.Abs(Input.GetAxis("Vertical")) != 0 && !isGuard){
PlayLadderSound();
}
}
function FixedUpdate(){
speed = (transform.position - lastPosition).magnitude;
lastPosition = transform.position;
}
function PlaySound(delay : float, volume : float){
if(isGuard){
if(!isPlaying){
audio.Stop();
audio.volume = volume * 0.75;
var num = Random.Range(0,3);
audio.clip = sounds[num];
isPlaying = true;
audio.Play();
yield WaitForSeconds(delay);
isPlaying = false;
}
}else if(!isPlaying && PlayerAnimScript.grounded && !PlayerScript.onLadder){
audio.Stop();
audio.volume = volume;
num = Random.Range(0,3);
audio.clip = sounds[num];
isPlaying = true;
audio.Play();
yield WaitForSeconds(delay);
isPlaying = false;
}
}
function PlayLadderSound(){
if(!isPlayingLadder){
audio.Stop();
audio.volume = ladderVolume;
var num = Random.Range(0,4);
Debug.Log(num);
audio.clip = ladderSounds[num];
isPlayingLadder = true;
audio.Play();
yield WaitForSeconds(ladderDelay);
isPlayingLadder = false;
}
}
function PlayJump(){
if(PlayerAnimScript.grounded && !isJumping){
isJumping = true;
AudioSource.PlayClipAtPoint(sounds[3], transform.position);
yield WaitForSeconds(0.3);
isJumping = false;
}
}
Answer by S_Darkwell · Mar 08, 2015 at 01:34 AM
The reason for your IndexOutOfRange exception is that Random.Range(0,4) will return one of the following: 0, 1, 2, 3, 4
The first sound in "ladderSounds" is actually at ladderSounds[0]. The last, if you add four sounds, is actually at ladderSounds[3].
The simple solution is to simply change:
var num = Random.Range(0,4);
--in your PlayLadderSound() function to:
var num = Random.Range(0,3);
A better solution, would be to replace that line instead with:
var num = Random.Range(0,ladderSounds.Length - 1);
This will automatically generate a random number within the boundaries of your list.
Hope that helps!