IndexOutOfRangeException error
Can't find nor solve what is causing the error.
IndexOutOfRangeException: Array index is out of range.Arrays.OnTriggerStay(UnityEngine.Collider other) (at Assets/Scripts/Arrays.cs:89)
This is the script using UnityEngine; using System.Collections;
public class Arrays : MonoBehaviour
{
public int numSelectors;
public int xPosition; //must be rounded to the nearest 10
public int yPosition;
public int zRotation;
public GameObject[] destruct;
public GameObject[] selectorArr;
public GameObject selector; //selected in the editor
public GameObject wirePrefab;
public bool verticalDirection;
public bool leftKeyPressed;
public bool rightKeyPressed;
public string deactivationName;
public bool wireSpawned;
public int total;
void Update () {
if (Input.GetKeyDown (KeyCode.LeftArrow)) {
//Debug.Log ("left key was pressed");
leftKeyPressed = true;
//Debug.Log ("true");
}
if (Input.GetKeyUp (KeyCode.LeftArrow)) {
//Debug.Log ("left key was released");
leftKeyPressed = false;
//Debug.Log ("false");
}
if (Input.GetKeyDown (KeyCode.RightArrow)) {
//Debug.Log ("right key was pressed");
rightKeyPressed = true;
//Debug.Log ("true");
}
if (Input.GetKeyUp (KeyCode.RightArrow)) {
//Debug.Log ("right key was released");
rightKeyPressed = false;
//Debug.Log ("false");
}
}
void Start()
{
}
void OnTriggerStay(Collider other){
selectorArr = new GameObject[numSelectors];
for (int i = 0; i < numSelectors; i++) {
GameObject[]destruct = GameObject.FindGameObjectsWithTag (deactivationName);
int destructCount = destruct.Length;
if (other.gameObject.tag == "Player") {
selector.gameObject.tag = deactivationName;
if (rightKeyPressed == true) {
if (wireSpawned == false) {
//if (GameObject.Find ("Pfb_PPlateOFF").GetComponent<PresurePlate> ().doorSpawned) { //will check if true
if (verticalDirection == false) {
GameObject go = Instantiate(selector, new Vector3((i * 16f) + xPosition, yPosition, -6), Quaternion.Euler(0,0,zRotation)) as GameObject;
go.transform.localScale = Vector3.one;
selectorArr[i] = go;
}
if (verticalDirection == true) {
GameObject go = Instantiate(selector, new Vector3(xPosition, (i * 16f) + yPosition, 0), Quaternion.Euler(0,0,zRotation)) as GameObject;
go.transform.localScale = Vector3.one;
selectorArr[i] = go;
}
if (destructCount > total) {
wireSpawned = true;
//Debug.Log("successful spawn");
}
}
}
}
if (leftKeyPressed == true) {
if (other.gameObject.tag == "Player") {
//if (wireSpawned == true){
Destroy (destruct [i]);
wireSpawned = false;
//foreach (GameObject goHolder in destruct) Destroy(goHolder);
//Destroy(go);
//Debug.Log("successful despawn");
//}
}
}
}
}
void OnTriggerExit(Collider other){
selector.gameObject.tag = ("wire");
}
}
Assistance and advices would be appreciated.
Thanks
$$anonymous$$oved to the new Help Room, which is the appropriate place for Debugging Qs.
Answer by cjdev · Aug 27, 2015 at 09:16 PM
On the line with Destroy (destruct [i]);
you're trying to access the array with an index greater than the length of the array so it's throwing the error. To solve it try checking if i < destruct.Length before trying to access it.
Your answer
Follow this Question
Related Questions
problems with trigger,some doubts about my script / algumas duvidas sobre o meu script 0 Answers
Trigger detects player collision and play specific audio from array 1 Answer
How can I collect collider2Ds(Trigger) the player is in into an array 1 Answer
Enable Multiple Box Collides From Parent Object 2 Answers
How do you rotate objects at the same time using arrays and triggers 0 Answers