Why RaycastHit.triangleIndex return -1 ?

I added a empty GameObject and added to it a Mesh Filter and Mesh Renderer components. And attached to it two scripts.

The first script create a mesh renderer plane.

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

The second script should cut holes in the Plane using the mouse button.

using UnityEngine;
using System.Collections;

public class CutHole : MonoBehaviour {

	// Use this for initialization
	void Start () {
	
	}
	
    void deleteTri(int index)
    {
        Destroy(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;
        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);
            }
        }
	}
}

I’m using a break point on the line:

deleteTri(hit.triangleIndex);

And triangleIndex is always -1

And another break point on the line

newTriangles[i++] = oldTriangles[j++];

This throw the exception:

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)

Do you also have a Mesh Collider on the GameObject?

From the RaycastHit.triangleIndex documentation:

Triangle index is only valid if the
collider that was hit is a
MeshCollider.