# SetSubMesh(int, SubMeshDescriptor, MeshUpdateFlags)

> Sets the information about a sub-mesh of the Mesh.

## Definition

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

```csharp
public void SetSubMesh(int index, SubMeshDescriptor desc, MeshUpdateFlags flags = MeshUpdateFlags.Default)
```

### Parameters

**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): Sub-mesh index. See [Mesh.subMeshCount](/engine/6000.5/script-reference/unityengine/mesh/submeshcount.md). Out of range indices throw an exception.**** (\[SubMeshDescriptor]\(/engine/6000.5/script-reference/unityengine/rendering/submeshdescriptor)): Sub-mesh data.**** (\[MeshUpdateFlags]\(/engine/6000.5/script-reference/unityengine/rendering/meshupdateflags)): Flags controlling the function behavior, see [MeshUpdateFlags](/engine/6000.5/script-reference/unityengine/rendering/meshupdateflags.md).

### Remarks

Note that SetSubMesh, [SubMeshDescriptor](/engine/6000.5/script-reference/unityengine/rendering/submeshdescriptor.md), and [Mesh.SetIndexBufferData](/engine/6000.5/script-reference/unityengine/mesh/setindexbufferdata.md) are designed for advanced users aiming for maximum performance, because they operate on the underlying mesh data structures that primarily work on raw index buffers, vertex buffers and mesh subset data. Using these methods, Unity performs very little data validation, so you must ensure your data is valid.

In particular, you must ensure that the SubMesh index range and topology are set to correct values. SubMesh indices, both indexStart and indexCount, must not overlap with any other SubMesh indices.

If using Mesh LODs, you must also ensure that the provided index range for each of the sub-meshes is large enough to contain all levels of detail used within that sub-mesh. You may be required to invoke [Mesh.SetLods](/engine/6000.5/script-reference/unityengine/mesh/setlods.md) prior to setting the subMesh. You can disable validation by setting flags to [MeshUpdateFlags.DontValidateLodRanges](/engine/6000.5/script-reference/unityengine/rendering/meshupdateflags/dontvalidatelodranges.md).

For information about the difference between the simpler and more advanced methods of assigning data to a Mesh from script, see the notes on the [Mesh](/engine/6000.5/script-reference/unityengine/mesh.md) page.

The SubMeshDescriptor-bounds, SubMeshDescriptor-firstVertex and SubMeshDescriptor-vertexCount values of [SubMeshDescriptor](/engine/6000.5/script-reference/unityengine/rendering/submeshdescriptor.md) are calculated automatically by `SetSubMesh`, unless [MeshUpdateFlags.DontRecalculateBounds](/engine/6000.5/script-reference/unityengine/rendering/meshupdateflags/dontrecalculatebounds.md) flag is passed. Note that this only applies to the bounds of the SubMesh. You must call [Mesh.RecalculateBounds](/engine/6000.5/script-reference/unityengine/mesh/recalculatebounds.md) to recalculate the bounds of the Mesh itself.

General usage pattern is:

```csharp
var mesh = new Mesh();

// setup vertex buffer data
mesh.vertices = ...;

// set index buffer
mesh.SetIndexBufferParams(...);
mesh.SetIndexBufferData(...);

// setup information about mesh subsets
mesh.subMeshCount = ...;
mesh.SetSubMesh(index, ...);

// update bounds of the mesh
mesh.RecalculateBounds();
```

For details on what data to set up for each sub-mesh, see [SubMeshDescriptor](/engine/6000.5/script-reference/unityengine/rendering/submeshdescriptor.md).

Additional Resources: [Mesh.subMeshCount](/engine/6000.5/script-reference/unityengine/mesh/submeshcount.md), [Mesh.GetSubMesh](/engine/6000.5/script-reference/unityengine/mesh/getsubmesh.md), [Mesh.SetIndexBufferParams](/engine/6000.5/script-reference/unityengine/mesh/setindexbufferparams.md), [Mesh.SetIndexBufferData](/engine/6000.5/script-reference/unityengine/mesh/setindexbufferdata.md), [MeshUpdateFlags](/engine/6000.5/script-reference/unityengine/rendering/meshupdateflags.md).
