- Home /
Array index out of range error
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class GridController : MonoBehaviour {
// Get closest free point in grid
public Vector3 snapPoint (GameObject GroundObject, Vector3 hit){
float worldX = 2F;
float worldY = 2F;
float worldZ = 2F;
bool IsGenerated = false;
float snap = 0.1F;
List<Vector3> possiblePositionsList = new List<Vector3>();
List<Vector3> tempPositionsList = new List<Vector3>();
List<float> distanceList = new List<float>();
int minIndex; // Shortest distance
float minVal;
this.transform.position = hit;
Vector3 position = this.transform.position;
position.x = Mathf.Round(position.x / snap) * snap;
position.y = Mathf.Round(position.y / snap) * snap;
position.z = Mathf.Round(position.z / snap) * snap;
this.transform.position = position;
Vector3 gridStart = transform.position - new Vector3(worldX / 2, worldY / 2, worldZ / 2);
for(float x =0F; x<worldX; x += snap) {
for(float y =0F; y<worldY; y += snap) {
for(float z =0F; z<worldZ; z += snap) {
Vector3 tempPosition;
tempPosition = new Vector3(gridStart.x + x, gridStart.y + y, gridStart.z + z);
tempPositionsList.Add(tempPosition);
}
}
}
IsGenerated = true;
foreach (Vector3 tempPosition in tempPositionsList){
GroundObject.transform.position = tempPosition;
bool isFree = true;
// Check is object free space WIP
Collider[] hitColliders = Physics.OverlapSphere(GroundObject.transform.position, GroundObject.collider.bounds.extents.magnitude);
foreach(Collider currentCollider in hitColliders){
if(GroundObject.collider.bounds.Intersects(currentCollider.bounds)){
int[] GroundObjectTriangles = GroundObject.transform.GetComponent<MeshFilter>().mesh.triangles;
int[] otherTriangles = currentCollider.transform.GetComponent<MeshFilter>().mesh.triangles;
Vector3[] GroundObjectVertices = GroundObject.transform.GetComponent<MeshFilter>().mesh.vertices;
Vector3[] otherVertices = currentCollider.transform.GetComponent<MeshFilter>().mesh.vertices;
int adjustedTriangleCount = GroundObjectTriangles.Length / 3;
int otherAdjustedTriangleCount = otherTriangles.Length / 3;
for(int i = 1; i < otherAdjustedTriangleCount; i ++){ // For every triangle in GroundObject...
Debug.Log(otherAdjustedTriangleCount);
Debug.Log((i - 1)*3);
Debug.Log(GroundObjectTriangles.Length);
for(int ib = 1; ib < adjustedTriangleCount; ib ++){ // For every triangle in other object...
int[] aPoints = new int[3];
aPoints[0] = GroundObjectTriangles[(i - 1)*3 + 0];
aPoints[1] = GroundObjectTriangles[(i - 1)*3 + 1];
aPoints[2] = GroundObjectTriangles[(i - 1)*3 + 2];
Vector3 a1 = GroundObject.transform.TransformPoint(GroundObjectVertices[aPoints[0]]);
Vector3 a2 = GroundObject.transform.TransformPoint(GroundObjectVertices[aPoints[1]]);
Vector3 a3 = GroundObject.transform.TransformPoint(GroundObjectVertices[aPoints[2]]);
int[] bPoints = new int[3];
bPoints[0] = otherTriangles[(ib - 1)*3 + 0];
bPoints[1] = otherTriangles[(ib - 1)*3 + 1];
bPoints[2] = otherTriangles[(ib - 1)*3 + 2];
Vector3 b1 = currentCollider.transform.TransformPoint(otherVertices[aPoints[0]]);
Vector3 b2 = currentCollider.transform.TransformPoint(otherVertices[aPoints[1]]);
Vector3 b3 = currentCollider.transform.TransformPoint(otherVertices[aPoints[2]]);
if(TriTriOverlap.TriTriIntersect(a1, a2, a3, b1, b2, b3)){
isFree = false;
}
}
}
}
}
if(isFree != false){
possiblePositionsList.Add (tempPosition);
}
}
foreach (Vector3 possiblePosition in possiblePositionsList){
distanceList.Add(Vector3.Distance(possiblePosition, transform.position));
}
minVal = distanceList.Min();
minIndex = distanceList.IndexOf(minVal);
return possiblePositionsList[minIndex];
}
}
In line 58. What's wrong? I tried to replace them with 0, 1 and 2 and it crashed.
IndexOutOfRangeException: Array index is out of range. GridController.snapPoint (UnityEngine.GameObject GroundObject, Vector3 hit) (at Assets/Game_scripts/GridController.cs:59) BuildScript.FixedUpdate () (at Assets/Game_scripts/BuildScript.cs:70)
Updated 2 times.
I don't see any out of bounds problem, but you should store the mesh arrays in local variables before manipulating them, and reassign them afterward. Unity's array, such as mesh.triangle, come from C++ and have to be copied each time you get them, which can become extremely heavy when looping over hundreds of iterations.
I'd split the assignment of the aPoints array into three lines to see exactly which line gets the error:
int[] aPoints = new int[3];
aPoints[0] = GroundObject.GetComponent<$$anonymous$$eshFilter>().mesh.triangles[(i - 1)*3 + 0];
aPoints[1] = GroundObject.GetComponent<$$anonymous$$eshFilter>().mesh.triangles[(i - 1)*3 + 1];
aPoints[2] = GroundObject.GetComponent<$$anonymous$$eshFilter>().mesh.triangles[(i - 1)*3 + 2];
In first for
loop, you use triangle count for collider and in the second for ground, but your comments suggest otherwise. If there are more triangles in collider, this will crash.
Answer by Paulius-Liekis · Jan 09, 2015 at 06:10 PM
You're using wrong index. This:
GroundObjectTriangles[(i - 1)*3 + 0];
should be:
GroundObjectTriangles[(ib - 1)*3 + 0];