# CombineMeshes(CombineInstance[], bool, bool, bool)

> Combines several Meshes into this Mesh.

## Definition

* **Type:** Method
* **Namespace:** [UnityEngine](/engine/6000.5/script-reference/unityengine.md)
* **Assembly:** UnityEngine.CoreModule

```csharp
public void CombineMeshes(CombineInstance[] combine, bool mergeSubMeshes, bool useMatrices, bool hasLightmapData)
```

### Parameters

**** (\[CombineInstance\[]]\(/engine/6000.5/script-reference/unityengine/combineinstance)): Descriptions of the Meshes to combine.**** (\[bool]\(https\://learn.microsoft.com/dotnet/api/system.boolean)): Defines whether Meshes should be combined into a single sub-mesh.**** (\[bool]\(https\://learn.microsoft.com/dotnet/api/system.boolean)): Defines whether the transforms supplied in the CombineInstance array should be used or ignored.**** (\[bool]\(https\://learn.microsoft.com/dotnet/api/system.boolean)): Defines whether to transform the input Mesh lightmap UV data using the lightmap scale offset data in [CombineInstance](/engine/6000.5/script-reference/unityengine/combineinstance.md) structs.

### Remarks

Combining meshes can improve performance by reducing the number of renderers needed to render a scene.

If `mergeSubMeshes` is set to true, all [CombineInstance](/engine/6000.5/script-reference/unityengine/combineinstance.md) instances are combined into a single sub-mesh. If set to false, each instance is placed in its own sub-mesh in the resulting mesh. Set this to true when all instances share the same material and topology for optimal performance.

When `useMatrices` is set to true, the vertices in each [CombineInstance](/engine/6000.5/script-reference/unityengine/combineinstance.md) are transformed with the instance’s [CombineInstance.transform](/engine/6000.5/script-reference/unityengine/combineinstance/transform.md) matrix. Otherwise, the transform matrices are ignored and the vertices are combined without modification. This parameter allows you to combine meshes into a common space. To ensure meshes align correctly in the combined mesh, temporarily reset the position and rotation of the combined Mesh's destination MeshFilter to zero before combining, and restore them afterward.

Set `hasLightmapData` to true to transform the input Mesh lightmap UV data using the lightmap scale offset data in [CombineInstance](/engine/6000.5/script-reference/unityengine/combineinstance.md) structs. The Meshes must share the same lightmap texture.

Note that meshes must be readable from the CPU to be combined. If a mesh is not CPU-readable, Unity logs a warning in the console and ignores the instance.

```csharp
using UnityEngine;

// This script combines the meshes from children into a single new Mesh.
// The combined mesh is assigned to the MeshFilter of this GameObject.
// The original meshes are not destroyed, but their GameObjects are deactivated.

[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
public class ExampleClass : MonoBehaviour
{
    void Start()
    {
        MeshFilter[] meshFilters = GetComponentsInChildren<MeshFilter>();
        CombineInstance[] instances = new CombineInstance[meshFilters.Length];

        for (int i = 0; i < meshFilters.Length; i++)
        {
            var meshFilter = meshFilters[i];

            instances[i] = new CombineInstance
            {
                mesh = meshFilter.sharedMesh,
                transform = meshFilter.transform.localToWorldMatrix,
            };

            meshFilter.gameObject.SetActive(false);
        }

        Mesh combinedMesh = new Mesh();
        combinedMesh.CombineMeshes(instances);
        gameObject.GetComponent<MeshFilter>().sharedMesh = combinedMesh;
        gameObject.SetActive(true);
    }
}
```

Additional Resources: [Mesh.isReadable](/engine/6000.5/script-reference/unityengine/mesh/isreadable.md), [MeshTopology](/engine/6000.5/script-reference/unityengine/meshtopology.md).
