unity MeshData api Help

Hi all I cant seem to get the new mesh data api stuff to work I followed the meshApi examples on github but that only has taking existing data , so I had to wing it a bit when making this script

The script generates an error saying its referencing out of bounds in verts,
also another wierd thing is I cant use color I have to use color32 which messes with the way i do my setting up .
I allocate my arrays in another thread so all that works and is report correct values ,
I only assign the meshdata to meshes which have > 0 vertices.
Its marching cubes BTW.
Heres what I have so far

public Mesh.MeshDataArray outputMesh;
        public List<Vector3> verts = new List<Vector3>();
        public List<Color> colorsM = new List<Color>();
        public List<Vector2> Uvs = new List<Vector2>();
        public List<int> tris = new List<int>();
        public List<Vector3> normals = new List<Vector3>();
        public void Initialize()
        {
            outputMesh = Mesh.AllocateWritableMeshData(1);

        }
void Execute(int o){
//size = verts.Count
outputMesh[0].SetVertexBufferParams(size, new VertexAttributeDescriptor(VertexAttribute.Position), new VertexAttributeDescriptor(VertexAttribute.Normal, stream: 1), new VertexAttributeDescriptor(VertexAttribute.TexCoord0), new VertexAttributeDescriptor(VertexAttribute.Color));
                    NativeArray<Vector3> NVerts = new NativeArray<Vector3>(size, Allocator.TempJob);
                    NVerts = outputMesh[0].GetVertexData<Vector3>();
                    NativeArray<Vector3> NNormals = new NativeArray<Vector3>(size, Allocator.TempJob); ;
                    NNormals = outputMesh[0].GetVertexData<Vector3>(stream: 1);
                    NativeArray<Vector2> NUVs = new NativeArray<Vector2>(size, Allocator.TempJob); ;
                    NUVs = outputMesh[0].GetVertexData<Vector2>();

            
                    NativeArray<Color32> NColors = new NativeArray<Color32>(size, Allocator.TempJob); ;
                    NColors = outputMesh[0].GetVertexData<Color32>();
            
                    var data = outputMesh[0];
                    int Tr = tris.Count;
                    outputMesh[0].SetIndexBufferParams(Tr, IndexFormat.UInt16);


                    NativeArray<int> NTris = new NativeArray<int>(Tr, Allocator.TempJob);
                    NTris = outputMesh[0].GetIndexData<int>();
              //tried setting VCount to SIze
                    int vCount = data.vertexCount;
                    for (int i = 0; i < vCount; i++)
                    {
                        NVerts[i] = verts[i];//this always says out of bounds
                        NUVs[i] = tUvs[i];
                        NColors[i] = color[i];


                    }
                    data.subMeshCount = 1;
                    outputMesh[0].SetSubMesh(0, new SubMeshDescriptor(0, Tr));
                    int T = data.GetSubMesh(0).indexCount;
            
                    for (int i = 0; i < T; i++)
                    {
                        NTris[i] = tris[i];

                    }
}

Thanks for your help

Closed not Solved.

Do you add values to your list “verts” ? Looks for me like it is always empty.

Yes Sorry I do but its all marching cubes so its done in another thread , I managed to fix the code but am unable to add colors and uvs to the mesh using the api.Heres the code now

//tris ,normals, verts , uvs , colors added in another thread these all work using the old api but assigning new api does not work for colors and uvs
//these are all assigned in marching cubes
                List<Vector3> tempVerts = new List<Vector3>();
                List<Vector3> tempNormals = new List<Vector3>();
                List<int> Tris = new List<int>();
                List<Color32> color = new List<Color32>();
                List<Vector2> tUvs = new List<Vector2>();

int T = Tris.Count;
                    var data = outputMesh[0];

                    data.SetVertexBufferParams(size, new VertexAttributeDescriptor(VertexAttribute.Position), new VertexAttributeDescriptor(VertexAttribute.Normal, stream: 1), new VertexAttributeDescriptor(VertexAttribute.Color, stream: 2), new VertexAttributeDescriptor(VertexAttribute.TexCoord0, stream: 3));
                    data.SetIndexBufferParams(T, IndexFormat.UInt32);


                    NativeArray<Vector3> NVerts;
                    NVerts = data.GetVertexData<Vector3>(stream: 0);
                    NativeArray<Vector3> NNormals;

                    NNormals = data.GetVertexData<Vector3>(stream: 1);

                    NativeArray<Color32> NColors;
                    NColors = data.GetVertexData<Color32>(stream: 2);

                    NativeArray<Vector2> NUVs;
                    NUVs = data.GetVertexData<Vector2>(stream: 3);

                    NativeArray<int> NTris;
                    NTris = data.GetIndexData<int>();

                    int vCount = NVerts.Length;


                    for (int i = 0; i < vCount; i++)
                    {
                    
                        NVerts[i] = tempVerts[i];
                        NNormals[i] = tempNormals[i];
                        NColors[i] = color[i];
                        NUVs[i] = tUvs[i];

                    }
                    for (int i = 0; i < T; i++)
                    {
                        NTris[i] = Tris[i];
                    }
                    var sm = new SubMeshDescriptor(0, T, MeshTopology.Triangles);
                    sm.firstVertex = 0;
                    sm.vertexCount = size;
                    data.subMeshCount = 1;

                    data.SetSubMesh(0, sm);

I apply the meshdata like so on the main thread

 Mesh.ApplyAndDisposeWritableMeshData(outputMesh, mesh, MeshUpdateFlags.Default);

I also initialize meshDataArray on the main thread before hand

Bump!

There’s not much to bump… if it is complaining that your triangles reference is out of bounds, that is exactly like an indexing error, just specific to the Mesh API.

If you have put in N vertices, then the only triangle indexes you can use from from 0 to N-1 inclusive.

Here’s my take on Index Out of range errors:

Here are some notes on IndexOutOfRangeException and ArgumentOutOfRangeException:

http://plbm.com/?p=236

oops bumped the wrong thread , I asked a question in answers , I managed to fix the out of bounds errors , but now its saying it cant capture the stream for colors and uvs(stream 2 & 3) , I’ll bump the relevant thread though