Why i'm getting Array index is out of range exception when trying to delete a triangle ?

What i’m trying to do is to create holes on a mesh plane.

First i have a script that create a mesh Plane. This script is attached to empty GameObject that i added to him a Mesh Filter and a Mesh Renderer.

using UnityEngine;
using System.Collections;

public class DragAndDrop : MonoBehaviour
{
    public float width = 5f;
    public float height = 5f;

    private Vector3[] vertices = new Vector3[4];
    private Mesh mesh;

    void Start()
    {
        MeshFilter mf = GetComponent<MeshFilter>();
        mesh = new Mesh();
        mf.mesh = mesh;

        int[] tri = new int[6];

        tri[0] = 0;
        tri[1] = 2;
        tri[2] = 1;

        tri[3] = 2;
        tri[4] = 3;
        tri[5] = 1;

        // Normals (Only if you want to display the object in the game)

        Vector3[] normals = new Vector3[4];

        normals[0] = Vector3.forward;
        normals[1] = Vector3.forward;
        normals[2] = Vector3.forward;
        normals[3] = Vector3.forward;

        // UVs (How textures are displayed)

        Vector2[] UV = new Vector2[4];

        UV[0] = new Vector2(0, 0);
        UV[1] = new Vector2(1, 0);
        UV[2] = new Vector2(0, 1);
        UV[3] = new Vector2(1, 1);

        // Assgin Arrays!


        mesh.vertices = vertices;
        mesh.triangles = tri;
        mesh.normals = normals;
        mesh.uv = UV;
    }

    void Update()
    {
        vertices = new[] { new Vector3(0, 0, 0), new Vector3(width, 0, 0), new Vector3(0, height, 0), new Vector3(width, height, 0) };
        mesh.vertices = vertices;
    }
}

Now i want to use the mouse button down to cut holes in the mesh plane i created.
So i’m using this script and is also attached to the empty GameObject but when i click with the mouse on the Plane i’m getting the exception message:

using UnityEngine;
using System.Collections;

public class CutHole : MonoBehaviour {

    // Use this for initialization
    void Start () {
   
    }
   
    void deleteTri(int index)
    {
        Destroy(this.gameObject.GetComponent<MeshCollider>());
        Mesh mesh = transform.GetComponent<MeshFilter>().mesh;

        int[] oldTriangles = mesh.triangles;
        int[] newTriangles = new int[mesh.triangles.Length - 3];

        int i = 0;
        int j = 0;

        while(j < mesh.triangles.Length)
        {
            if(j != index*3)
            {
                newTriangles[i++] = oldTriangles[j++];
                newTriangles[i++] = oldTriangles[j++];
                newTriangles[i++] = oldTriangles[j++];
            }
            else
            {
                j += 3;
            }
        }
        transform.GetComponent<MeshFilter>().mesh.triangles = newTriangles;
        this.gameObject.AddComponent<MeshCollider>();
    }

    // Update is called once per frame
    void Update ()
    {
     if (Input.GetMouseButtonDown(0))
        {
            RaycastHit hit;

            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if(Physics.Raycast(ray, out hit, 1000.0f))
            {
                deleteTri(hit.triangleIndex);
            }
        }
    }
}

The exception is on the second script on the line 26:

IndexOutOfRangeException: Array index is out of range.
CutHole.deleteTri (Int32 index) (at Assets/MyScripts/CutHole.cs:26)
CutHole.Update () (at Assets/MyScripts/CutHole.cs:82)

Maybe the problem or one of the problems is in the first script where i’m updating the vertices all the time ?

void Update()
   {
       vertices = new[] { new Vector3(0, 0, 0), new Vector3(width, 0, 0), new Vector3(0, height, 0), new Vector3(width, height, 0) };
        mesh.vertices = vertices;
   }

But i doubt this is the problem that cause the exception error.

I believe the error is happening on “newTriangles[i++] = oldTriangles[j++];”, correct?
One way that could happen is that if index is not hit (by being negative or too high) then it will try to add all the triangles in oldTriangles to newTriangles, but since the latter is three smaller, that won’t work.

1 Like