- Home /
Using a footstep C# script and keep getting an "IndexOutOfRangeException"
Hi there,
I am in the middle of finishing off a test environment that I am putting together as part of my Sound Design Portfolio. I have added some footstep sounds to the player by using the script that can be found in this youtube vid: https://www.youtube.com/watch?v=0nbNyJ7Vl-Y
I keep getting an "IndexOutOfRangeException: Array Index is out of range." error each time the player bumps into the wall that goes around the perimeter of the environment that I have made. The script works fine until that error occurs.
Can any of you chaps a Unity novice like me out? :)
I can paste the code here if you guys would prefer, but it has quite a lot of lines of code. But the C# script linked in the description of the YouTube vid that I pasted further up is pretty much untouched in my project.
Thanks :)
Answer by Graham-Dunnett · Sep 19, 2014 at 03:22 PM
Well, Index out of Range, means you are trying to access an array element that doesn't exist. If your array had 10 elements, and you tried to access the 11th element, you'd get this error. Also, if your array doesn't exist yet (because it's not been created) and you try and access it, you'll get the same error. Usually this means you ask how long the array is, get told zero, and try to access element zero. Further, the error message will include the exact line of code where the problem happens, so tracking down which array is causing the problems should be easy.
Answer by dmg0600 · Sep 19, 2014 at 03:49 PM
If you have any tag setted so the sound for that tag can be played, you must have some AudioClips in the array for that certain tag.
This is because it tries to play the sound choosing the AudioClip from the ones in the array. If the array is null it selects a random number between 0 and the length of the array, which is 0. Therefore it tries to access arrayOfSounds[0] which is not correct as that array has no AudioClips.
The script I'm using had several tags for surfaces for the player to walk on, "Concrete", "Dirt", "Floor", "Grass" etc. I am only using "Grass" (For the moment). I apologise, in my naivety, I have now tried gutting the script from it's original form so the only tag that remains is "Grass". But now I am getting compiler errors.
Here is the code:
Any advice on what I could do to tweak it would be appreciated :) I seriously need to learn C# more in depth.
var grass: AudioClip [];
private var step : boolean = true;
var audioStepLengthWalk : float = 0.45;
var audioStepLengthRun : float = 0.25;
function OnControllerColliderHit (hit : ControllerColliderHit) {
var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded && controller.velocity.magnitude < 7 && controller.velocity.magnitude > 5 && hit.gameObject.tag == "Grass" && step == true || controller.isGrounded && controller.velocity.magnitude < 7 && controller.velocity.magnitude > 5 && hit.gameObject.tag == "Untagged" && step == true ) {
WalkOnGrass();
} else if (controller.isGrounded && controller.velocity.magnitude < 7 && controller.velocity.magnitude > 5 && hit.gameObject.tag == "Grass" && step == true) {
}
////////////////////////////////// GRASS ///////////////////////////////////////////////
function WalkOnGrass() {
step = false;
audio.clip = grass[Random.Range(0, grass.length)];
audio.volume = .1;
audio.Play();
yield WaitForSeconds (audioStepLengthWalk);
step = true;
}
You are missing a } after else if (...) {
but as you don't even need the else, just remove it.
var grass: AudioClip [];
private var step : boolean = true;
var audioStepLengthWalk : float = 0.45;
var audioStepLengthRun : float = 0.25;
function OnControllerColliderHit (hit : ControllerColliderHit) {
var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded && controller.velocity.magnitude < 7 && controller.velocity.magnitude > 5 && hit.gameObject.tag == "Grass" && step == true || controller.isGrounded && controller.velocity.magnitude < 7 && controller.velocity.magnitude > 5 && hit.gameObject.tag == "Untagged" && step == true ) {
WalkOnGrass();
}
}
////////////////////////////////// GRASS ///////////////////////////////////////////////
function WalkOnGrass() {
step = false;
audio.clip = grass[Random.Range(0, grass.length)];
audio.volume = .1;
audio.Play();
yield WaitForSeconds (audioStepLengthWalk);
step = true;
}
Your answer
Follow this Question
Related Questions
Problem with World Generate 1 Answer
[SOLVED] Problem with "foreach". 1 Answer
Multiple Cars not working 1 Answer
Can't get sound clip from array to play properly 1 Answer
Distribute terrain in zones 3 Answers