# How to avoid repetitive code for static mesh and skeletal mesh components

**URL:** <https://forums.unrealengine.com/t/how-to-avoid-repetitive-code-for-static-mesh-and-skeletal-mesh-components/503498>\
**Category:** C++\
**Tags:** unreal-engine, UE4, Blueprint\
**Created:** [March 2, 2022, 7:38pm UTC](https://forums.unrealengine.com/t/how-to-avoid-repetitive-code-for-static-mesh-and-skeletal-mesh-components/503498 "2022-03-02T19:38:05Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![JaredTherriault](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jaredtherriault/32/1194499_2.png) [@JaredTherriault](https://forums.unrealengine.com/u/JaredTherriault)\
**Post date:** [March 2, 2022, 8:59pm UTC](https://forums.unrealengine.com/t/how-to-avoid-repetitive-code-for-static-mesh-and-skeletal-mesh-components/503498/2 "2022-03-02T20:59:59Z")

</div>

You could use UMeshComponent from which both UStaticMeshComponent and USkeletalMeshComponent derive.

```auto
TSet<UMeshComponent*> MeshComponents;

if (StaticMeshComponents.Num())
{
    for (UStaticMeshComponent* sm : StaticMeshComponents)
    {
        MeshComponents.Add(sm);
    }
}
else
{
    for (USkeletalMeshComponent* sm : SkeletalMeshComponents)
    {
        MeshComponents.Add(sm);
    }
}

for (UMeshComponent* mc : MeshComponents)
{
	if (UMaterialInterface* DefaultMaterial = mc->GetMaterial(IndexOfMaterialToMakeDynamic))
	{
		DynamicMaterialInstance = UMaterialInstanceDynamic::Create(DefaultMaterial, this);
		mc->SetMaterial(IMaterialDynamic, DynamicMaterialInstance);
	}
}

```

* * *

[Runtime DataTable](https://www.unrealengine.com/marketplace/en-US/product/4c7c4f8d523f4c90b2e26d23502c526c) : Import and export game data to and from CSV or Google Sheets while your game is running!

[GSheets Operator](https://www.unrealengine.com/marketplace/en-US/product/aa61e3df41fa46629d73ab59cacf9429) : Interface with Google Sheets, add new and delete old sheets, edit columns and rows, make in-game spreadsheets and more!

[easyCSV](https://www.unrealengine.com/marketplace/en-US/product/b25b855096e74d088d7553ccbe4072f2) : Read data from any CSV and put them in an easy-to-access string map!

[iTween](https://forums.unrealengine.com/t/open-beta-procedural-on-the-fly-animation-in-ue4-itween/10472) : Free, smooth, procedural object animation

---

_[View the full topic](https://forums.unrealengine.com/t/how-to-avoid-repetitive-code-for-static-mesh-and-skeletal-mesh-components/503498)._
