I seem to be getting this error:
IndexOutOfRangeException: Array index is out of range.
Footstep.WalkOnWood () (at Assets/_Scripts/Footstep.cs:59)
Footstep.OnControllerColliderHit (UnityEngine.ControllerColliderHit hit) (at Assets/_Scripts/Footstep.cs:29)
UnityEngine.CharacterController:Move(Vector3)
CharacterMotor:UpdateFunction() (at Assets/Standard Assets/Character Controllers/Sources/Scripts/CharacterMotor.js:229)
CharacterMotor:FixedUpdate() (at Assets/Standard Assets/Character Controllers/Sources/Scripts/CharacterMotor.js:331)
When I walk on Concrete, it plays the audio fine with no errors. But when I transition from walking on Concrete to Wood, I instantly get this error. Can anybody tell me why? Here’s the script:
using UnityEngine;
using System.Collections;
public class Footstep : MonoBehaviour {
public CharacterController controller;
public AudioClip[] concrete;
public AudioClip[] wood;
public AudioClip[] dirt;
public AudioClip[] metal;
public AudioClip[] glass;
public AudioClip[] sand;
public AudioClip[] snow;
public AudioClip[] floor;
public AudioClip[] grass;
private bool step = true;
float audioStepLengthWalk = 0.7f;
float audioStepLengthRun = 0.7f;
void OnControllerColliderHit ( ControllerColliderHit hit) {
if (controller.isGrounded && controller.velocity.magnitude < 10 && controller.velocity.magnitude > 5 && hit.gameObject.tag == "Untagged" && step == true ) {
WalkOnConcrete();
} else if (controller.isGrounded && controller.velocity.magnitude > 8 && hit.gameObject.tag == "Concrete" && step == true || controller.isGrounded && controller.velocity.magnitude > 10 && hit.gameObject.tag == "Untagged" && step == true) {
RunOnConcrete();
}
if (controller.isGrounded && controller.velocity.magnitude < 10 && controller.velocity.magnitude > 5 && hit.gameObject.tag == "Wood" && step == true ) {
WalkOnWood();
} else if (controller.isGrounded && controller.velocity.magnitude > 8 && hit.gameObject.tag == "Wood" && step == true || controller.isGrounded && controller.velocity.magnitude > 10 && hit.gameObject.tag == "Untagged" && step == true) {
RunOnWood();
}
}
IEnumerator WaitForFootSteps(float stepsLength) {
step = false;
yield return new WaitForSeconds(stepsLength);
step = true; }
/////////////////////////////////// CONCRETE ////////////////////////////////////////
void WalkOnConcrete() {
audio.clip = concrete[Random.Range(0, concrete.Length)];
audio.volume = 0.65f;
audio.Play();
StartCoroutine(WaitForFootSteps(audioStepLengthWalk));
}
void RunOnConcrete() {
audio.clip = concrete[Random.Range(0, concrete.Length)];
audio.volume = 0.65f;
audio.Play();
StartCoroutine(WaitForFootSteps(audioStepLengthWalk));
}
/////////////////////////////////// Wood ////////////////////////////////////////
void WalkOnWood() {
audio.clip = wood[Random.Range(0, concrete.Length)];
audio.volume = 0.75f;
audio.Play();
StartCoroutine(WaitForFootSteps(audioStepLengthWalk));
}
void RunOnWood() {
audio.clip = wood[Random.Range(0, concrete.Length)];
audio.volume = 0.75f;
audio.Play();
StartCoroutine(WaitForFootSteps(audioStepLengthWalk));
}
}