Failed setting triangles. Some indices are referencing out of bounds vertices. (424660)

Hi there
I’ve ran into this error while writing a runtime loader for custom 3d model format. Actually the loader is pretty much done, but I get the error when I’m testing the script.

Failed setting triangles. Some indices are referencing out of bounds vertices.

I know it’s probably common sense, but I’m not very good programmer so I don’t get it lol. Could someone point out what I’m doing wrong, please? Here’s the source:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ModelLoader {
    
    public ModelLoader(int model_id, string objectname) {
        this.model_id = model_id;
        this.objectname = objectname;
        if(!cached_models.ContainsKey(model_id))
            parse_footer();
    }
    
    private void parse_footer() {
        data = DataBuffer.load("/New Unity Project 6/modelz/"+ model_id + ".rsm");
        data.set_caret(data.get_length() - 23);
        
        num_vertices = data.read_short();
        num_triangles = data.read_short();
        num_tex_triangles = data.read_byte();
        
        int use_tex = data.read_byte();
        bool bools = (0x1  use_tex ^ 0xffffffff) == -2;
        bool bool_78_ = (use_tex  0x2 ^ 0xffffffff) == -3;
        int use_tri_priority = data.read_byte();
        int use_transparency = data.read_byte();
        int use_tri_skinning = data.read_byte();
        int numtrilen2 = data.read_byte();
        int use_vert_skinning = data.read_byte();
        
        int x_data_len = data.read_short();
        int y_data_len = data.read_short();
        int z_data_len = data.read_short();
        int tri_data_len = data.read_short();
        int unknown = data.read_short();        
        Debug.Log("vertices: " + num_vertices + " //triangles: " + num_triangles);
        
    
        
int tmp = 0;
        vert_dir_offset = tmp;
        tmp += num_vertices;
        tex_pointer_offset = tmp;
        if(use_tex == 1)
            tmp += num_triangles;
        tri_type_offset = tmp;
        tmp += num_triangles;
        tri_priority_offset = tmp;
        if(use_tri_priority == 255)
            tmp += num_triangles;
        tri_skin_offset = tmp;
        if(use_tri_skinning == 1)
            tmp += num_triangles;
        vert_skin_offset = tmp;
        if(use_vert_skinning == 1)
            tmp += num_vertices;
        tri_alpha_offset = tmp;
        if(use_transparency == 1)
            tmp += num_triangles;
        tri_data_offset = tmp;
        tmp += tri_data_len;
        numtrilen2_offset = tmp;
        if(numtrilen2 == 1)
            tmp += num_triangles * 2;
        unknown_offset = tmp;
        tmp += unknown;
        color_data_offset = tmp;
        tmp += num_triangles * 2;
        x_data_offset = tmp;
        tmp += x_data_len;
        y_data_offset = tmp;
        tmp += y_data_len;
        z_data_offset = tmp;
        tmp += z_data_len;
        uv_map_offset = tmp;
        tmp += k4 * 6;
        k9 = tmp;
        tmp += l4 * 6;
        l9 = tmp;
        tmp += l4 * 6;
        i10 = tmp;
        tmp += l4;
        j10 = tmp;
        tmp += l4;
        k10 = tmp;
        tmp += l4 + i5 * 2;
    }
    
    public Mesh generate_mesh() {
        if(cached_models == null)
            cached_models = new Dictionary<int, Mesh>();
        
        if(cached_models.ContainsKey(model_id))
            return cached_models[model_id];
        
        Mesh _mesh = new Mesh();
        Vector3[] _vertices = new Vector3[num_vertices];
        Vector2[] _uv = new Vector2[num_vertices];
        
        int[] vertexX = new int[num_vertices];
        int[] vertexY = new int[num_vertices];
        int[] vertexZ = new int[num_vertices];
        int[] facePoint1 = new int[num_triangles];
        int[] facePoint2 = new int[num_triangles];
        int[] facePoint3 = new int[num_triangles];
        

        

        int[] _triangles = new int[num_triangles * 3];
        
        data.set_caret(vert_dir_offset);
        
        DataBuffer stream_1 = new DataBuffer(data.get_buffer());
        DataBuffer stream_2 = new DataBuffer(data.get_buffer());
        DataBuffer stream_3 = new DataBuffer(data.get_buffer());
        
                DataBuffer nc5 = new DataBuffer(data.get_buffer());
        DataBuffer nc6 = new DataBuffer(data.get_buffer());
        DataBuffer nc7 = new DataBuffer(data.get_buffer());
        
        
        stream_1.set_caret(x_data_offset);
        stream_2.set_caret(y_data_offset);
        stream_3.set_caret(z_data_offset);
        nc5.caret = vert_skin_offset;
        
        int x_off = 0;
        int y_off = 0;
        int z_off = 0;
        for(int i = 0; i < num_vertices; i++) {
            int flag = data.read_byte();
            
            int cur_x_off = 0;
            if((flag  1) != 0)
                cur_x_off = stream_1.read_smart();
            
            int cur_y_off = 0;
            if((flag  2) != 0)
                cur_y_off = stream_2.read_smart();
            
            int cur_z_off = 0;
            if((flag  4) != 0)
                cur_z_off = stream_3.read_smart();
            
    
            _uv[i] = new Vector2(0, 0);

            vertexX[i] = x_off + cur_x_off;
            vertexY[i] = y_off + cur_y_off;
            vertexZ[i] = z_off + cur_z_off;
            x_off = vertexX[i];
            y_off = vertexY[i];
            z_off = vertexZ[i];
            
            
            _vertices[i] = new Vector3(vertexX[i], vertexY[i], vertexZ[i]);
            
        }
        
        //data.set_caret(tri_data_offset);
        data.caret = color_data_offset;
        stream_1.caret = tex_pointer_offset;
        stream_2.caret = tri_priority_offset;
        stream_3.caret = tri_alpha_offset;
        
                nc5.caret = tri_skin_offset;
        nc6.caret = numtrilen2_offset;
        nc7.caret = unknown_offset;
        
        int offset_1 = 0;
        int offset_2 = 0;
        int offset_3 = 0;
        int start_val = 0;
        int tri_idx = 0;
        
        for(int i = 0; i < num_triangles; i++) {
            int tri_type = stream_1.read_byte();
            if(tri_type == 1) {
                offset_1 = data.read_smart()+ start_val;
                start_val = offset_1;
                offset_2 = data.read_smart()+ start_val;
                start_val = offset_2;
                offset_3 = data.read_smart()+ start_val;
                start_val = offset_3;
                
                facePoint1[i] = offset_1;
                facePoint2[i] = offset_2;
                facePoint3[i] = offset_3;
                
            } else if(tri_type == 2) {
                offset_2 = offset_3;
                offset_3 = data.read_smart()+ start_val;
                start_val = offset_3;
                
                facePoint1[i] = offset_1;
                facePoint2[i] = offset_2;
                facePoint3[i] = offset_3;
                
            } else if(tri_type == 3) {
                offset_1 = offset_3;
                offset_3 = data.read_smart()+ start_val;
                start_val = offset_3;
                
                facePoint1[i] = offset_1;
                facePoint2[i] = offset_2;
                facePoint3[i] = offset_3;
                
            } else if(tri_type == 4) {
                int tmp = offset_1;
                offset_1 = offset_2;
                offset_2 = tmp;
                offset_3 = data.read_smart()+ start_val;
                start_val = offset_3;
                
                facePoint1[i] = offset_1;
                facePoint2[i] = offset_2;
                facePoint3[i] = offset_3;
            }

            
            
_triangles[tri_idx++] = facePoint1[i] + facePoint2[i] + facePoint3[i];

            

        }
        
        data.caret = uv_map_offset;
        stream_1.caret = k9;
        stream_2.caret = l9;
        stream_3.caret = i10;
        nc5.caret = j10;
        nc6.caret = k10;
        
        
        _mesh.vertices = _vertices;
        _mesh.triangles = _triangles;
        //_mesh.uv = _uv;
        //_mesh.colors = _colors;
        _mesh.RecalculateNormals();
        _mesh.RecalculateBounds();
        
        cached_models[model_id] = _mesh;
        return _mesh;
    }
    
            public int k9;
        public int l9;
        public int i10;
        public int j10;
        public int k10;
    
    byte[] x = null;
        byte[] O = null;
        byte[] J = null;
        byte[] F = null;
        byte[] cb = null;
        byte[] gb = null;
        byte[] lb = null;
        int[] ab = null;
        int[] kb = null;
        int[] y = null;
        int[] N = null;
        short[] D = null;

    
                int k4 = 0;
        int l4 = 0;
        int i5 = 0;
        int v = 0;
        int hb = 0;
        int P = 0;
        byte G = 0;
    
            int[] texTrianglesPoint1 = null;
        int[] texTrianglesPoint2 = null;
        int[] texTrianglesPoint3 = null;
    
    private string objectname;
    private int model_id;
    private DataBuffer data;
    private int num_vertices;
    private int num_triangles;
    private int num_tex_triangles;
    private int vert_dir_offset;
    private int tri_type_offset;
    private int tri_priority_offset;
    private int tri_skin_offset;
    private int tex_pointer_offset;
    private int vert_skin_offset;
    private int tri_alpha_offset;
    private int tri_data_offset;
    private int color_data_offset;
    private int uv_map_offset;
    private int x_data_offset;
    private int y_data_offset;
    private int z_data_offset;
    private int unknown_offset;
    private int numtrilen2_offset;
    
    private static Dictionary<int, Mesh> cached_models = new Dictionary<int, Mesh>();
}

Thanks

The error most likely means that for a mesh with N vertices, you have one or more indices with values >= N.

As to why, it’s probably due to an error in your code. If you’re using Unity 3, you might be able to make use of the new debugging features to track down the cause of the error. Otherwise, you may need to add some debug output to try to narrow things down. You could check all the indices manually, and for any that are out of bounds, print out their values (and the number of vertices for comparison) using Debug.Log(). From there, you can try to determine where and why the invalid indices are being generated.

1 Like

Thanks for the answer, but I’ve already figured it out and finished the loader :wink:

If you already figured out, you should have posted about it.

It is so frustrating when you are searching for a solution to your problem and the thread starter don’t post back to explain the actual Solution!

2 Likes

agreed! I’m trying to write a custom loader as well and could use a lil help with this error!

Simple, stop making your triangles refer to vertices that don’t exist. :wink: Actually it is that simple, but why that’s happening depends on the logic of your code. Basically, what Jesse said.

–Eric

1 Like

Maybe I should have stated my problem before in case someone could use it, I eventually found the solution. I was copying arrays of Vertices and Indices in order to modify part of the mesh. What happened is when I copied back my triangles inside the Unity object it was referencing to outbounds index because they had been changed in the modifying process.

So be careful when you are doing modification in those stuff. You shall ensure that your index are changed everywhere before reloading them.

A detailed explanation can be found here.

one way to recreate this problem is , when setting the mesh triangles at the end of the procedural code, to set them before you have set the vertices etc. write the triangles array into the mesh after having written the vertices in.

Quick note, don’t set triangles before you set vertices in the mesh.

1 Like

I get this error using only Unity 5.0.2f1 and the default blender import script. It happens when my blender mesh has Shading set to Smooth in Object mode.

smooth shading perhaps merges some vertices.

Some meshes I have imported from Blender bring this up in play mode if they are on static objects. Unfortunately the error doesn’t tell me which mesh is causing the problem. Also, I have no idea what to do to ‘fix’ the mesh - mucking about with smoothing etc in Blender doesn’t seem to help.

Please make a new post - it’s unlikely your problem is the same problem as this very, very old question.

Sure, I’ll do that. But it is the same error message - “Failed Setting Traingles, some indices are referencing out of bounds vertices”, so I surmise that though perhaps the cause may be different, the problem is the same.

anyone still running into this problem? try this:
after creating a new Mesh, do Mesh.Clear() before setting any vertices, normals, UVs or triangles.
for some reason this works…
Mesh mesh = new Mesh();
mesh.Clear();
mesh.SetVertices(vertices);
etc…

1 Like

For those still getting this error: I had this problem because my code set triangles before vertices, so the simple fix in my case was to swap the order back around.

Won’t Work:
mesh1.triangles = Triangles.ToArray();
mesh1.vertices = Vertices.ToArray();

Will Work:
mesh1.vertices = Vertices.ToArray();
mesh1.triangles = Triangles.ToArray();