Combine meshes with submeshes help

I tried, but I couldn’t figure it out. Google and Answers got me close, but I need help. I’m trying to combine multiple instanced meshes, each having two submeshes. The following code creates multiple game objects, one for each submesh in the instance. For example, I’m trying to combine a group of 10 trees, each tree having a trunk material and a leaf material. The result of the code leaves me with two gameobjects, one of all the trunks combined, and one of all the leaves combined. I would like to end up with one gameobject, of all the trunks and leaves combined, and the two materials.

void Start()
    {
        //create an empty gameobject, add this script to it
        //attach multiple gameobject clones to this gameobject (all having the same mesh and multiple materials)
        //run program
        if (gameObject.transform.childCount > 1)
        {
            //line up placement of children around empty parent
            float width = gameObject.transform.GetChild(0).GetComponent<MeshFilter>().mesh.bounds.size.x;
            for (int i = 0; i < gameObject.transform.childCount; i++)
            {
                gameObject.transform.GetChild(i).transform.localPosition = new Vector3(i * width - (width * gameObject.transform.childCount / 2f), 0, 0);
                gameObject.transform.GetChild(i).transform.localEulerAngles = new Vector3(Random.Range(-5f, 5f), Random.Range(0, 360), Random.Range(-5f, 5f));
            }

            //get materials from first child (every child is the same)
            Material[] material = gameObject.transform.GetChild(0).GetComponent<MeshRenderer>().materials;

            CombineInstance ci;
            MeshFilter[] meshFilters = gameObject.GetComponentsInChildren<MeshFilter>();
            List<CombineInstance> combine = new List<CombineInstance>();
            GameObject[] parent = new GameObject[meshFilters[0].sharedMesh.subMeshCount];
            for (int i = 0; i < parent.Length; i++)
            {
                parent *= new GameObject();*

parent*.name = "Submesh " + i;*

for (int ii = 0; ii < meshFilters.Length; ii++)
{
if (meshFilters*.sharedMesh != null)*
{
ci = new CombineInstance();
ci.mesh = meshFilters[ii].sharedMesh;
ci.subMeshIndex = i;
ci.transform = meshFilters[ii].transform.localToWorldMatrix;
combine.Add(ci);
}
else
{
ci = new CombineInstance();
ci.mesh = new Mesh();
combine.Add(ci);
}
}

parent*.AddComponent().mesh.CombineMeshes(combine.ToArray());*
parent_.AddComponent().material = material*;
}*_

//destroy all of original gameobjects; they are no longer needed.
for (int ii = 0; ii < gameObject.transform.childCount; ii++)
{
Destroy(gameObject.transform.GetChild(ii).gameObject);
}

//attatch newly created gameobjects to main gameobject to keep things clean
for (int i = 0; i < parent.Length; i++)
{
parent*.transform.parent = gameObject.transform;*
}
}
else
{
Debug.Log(“NOT ENOUGH CHILDREN TO COMBINE”);
}
}

Got it:

    public GameObject obj;
    [Range(0, 100)]
    public int amount;

	// Use this for initialization
	void Start ()
    {
        Duplicate();
    }
	
    private void Duplicate()
    {
        Mesh mainMesh = obj.GetComponent<MeshFilter>().sharedMesh;

        Mesh meshToCopy = new Mesh();
        meshToCopy.vertices = mainMesh.vertices;
        meshToCopy.triangles = mainMesh.triangles;
        meshToCopy.normals = mainMesh.normals;
        meshToCopy.uv = mainMesh.uv;

        Matrix4x4[] matrix = new Matrix4x4[amount];
        for (int i = 0; i < amount; i++)
        {
            matrix_.SetTRS(new Vector3(i * 10, 0, 0), Quaternion.Euler(new Vector3(Random.Range(-5, 5), Random.Range(0, 360), Random.Range(-5, 5))), Vector3.one);_

}

CombineInstance[] ci = new CombineInstance[amount];
for (int i = 0; i < amount; i++)
{
ci = new CombineInstance();
ci*.mesh = meshToCopy;*
ci_.transform = matrix*;
}*_

Material[] materials = obj.GetComponent().sharedMaterials;

Mesh batchedMesh = gameObject.AddComponent().mesh = new Mesh();
batchedMesh.CombineMeshes(ci);
gameObject.AddComponent().materials = materials;

int[] mainSubTri;
int[] newSubTri;
batchedMesh.subMeshCount = mainMesh.subMeshCount;

for (int i = 0; i < mainMesh.subMeshCount; i++)
{
mainSubTri = mainMesh.GetTriangles(i);

newSubTri = new int[mainSubTri.Length * amount];

for (int ii = 0; ii < amount; ii++)
{
for (int iii = 0; iii < mainSubTri.Length; iii++)
{
newSubTri[(ii * mainSubTri.Length) + iii] = mainSubTri[iii] + (ii * mainMesh.vertexCount);
}
}
batchedMesh.SetTriangles(newSubTri, i);
}
}