Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Avash · Jan 09, 2015 at 02:29 PM · c#errorarray

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.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Berenger · Jan 09, 2015 at 02:46 PM 0
Share

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.

avatar image Baste · Jan 09, 2015 at 03:02 PM 0
Share

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];
avatar image ArkaneX · Jan 09, 2015 at 04:10 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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];


Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Avash · Jan 10, 2015 at 02:59 PM 0
Share

Thank you.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

29 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Picking a Random Order for Levers 1 Answer

SetActive() Object reference not set to an instance of an object. 1 Answer

A problem with intersection detection 1 Answer

Semicolons are seen as an unexpected symbol 1 Answer

Few problems with arrays 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges