Why is my submesh not rendering properly

i have 2 meshes that ive tried to turn into a 2 submeshes but it just sort of combines them when i do it, does anyone know why?

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

public class plane : MonoBehaviour
{
    public GameObject obmesh1;
    public GameObject obmesh2;
    public int[] triangles1;
    public int[] triangles2;
    public Vector3[] verticies1;
    public Vector3[] verticies2;
    public Vector3[] allverticies;
    public Vector2[] uvs1;
    public Vector2[] uvs2;
    public Vector2[] alluvs;


    // Start is called before the first frame update
    void Start()
    {
       
        Mesh mesh1 = obmesh1.GetComponent<MeshFilter>().mesh;
        Mesh mesh2 = obmesh2.GetComponent<MeshFilter>().mesh;
        Mesh mesh = GetComponent<MeshFilter>().mesh;
        //mesh.Clear();
        mesh.subMeshCount = 2;
        Debug.Log("Submeshes: " + mesh.subMeshCount);
       
        int[] triangles1 = mesh1.GetTriangles(0);
        int[] triangles2 = mesh2.GetTriangles(0);
        verticies1 = mesh1.vertices;
        verticies2 = mesh2.vertices;
       
        allverticies = verticies1.Concat(verticies2).ToArray();

        uvs1 = mesh1.uv;
        uvs2 = mesh2.uv;
        alluvs = uvs1.Concat(uvs2).ToArray();
        mesh.uv = alluvs;
       
        mesh.vertices = allverticies;
       
        mesh.SetTriangles(triangles1, 0);
        mesh.SetTriangles(triangles2, 1);
        mesh.RecalculateBounds();
        mesh.RecalculateNormals();
      
    }

}

8376903--1104369--mesh.png

You have not adjusted the indices of your second submesh. Keep in mind that the triangles consist of indices into the vertices array. Since you concat your vertices into a single array, the vertices of the second mesh would be offset.

Just as the most simple example. Assume you have two meshes and each mesh only has 3 vertices and a single triangle. So for the first mesh you would have the vertices va0, va1, va2 and a triangles array that looks like [0, 1, 2]. The second mesh would look pretty much the same but has the vertices vb0, vb1, vb2.

After combining them you have a single vertex array with 6 elements that looks like this va0, va1, va2, vb0, vb1, vb2. The two triangle arrays however need to be adjusted to: [0, 1, 2] for the first submesh and [3, 4, 5] for the second one since the vertices for the second submesh start at index 3, after the first one.

ive tried to do this to off set it, but it gives error:
Failed setting triangles. Some indices are referencing out of bounds vertices. IndexCount: 600, VertexCount: 145
UnityEngine.Mesh:SetTriangles (int[ ],int)

for (int i = 0; i < triangles2.Length; i++)
        {
            triangles2[i] = triangles2[i] + triangles1.Length;
        }

This makes no sense :slight_smile: The indices refer to the vertices array. So adding the length of the triangles array makes no sense. You should add the length of the vertices array instead.

Thankyou it all works now :slight_smile: