# Setting Dynamic Material Instance for Skeletal/Static meshes using C++?

**URL:** <https://forums.unrealengine.com/t/setting-dynamic-material-instance-for-skeletal-static-meshes-using-c/552079>\
**Category:** C++\
**Tags:** UE4-20, unreal-engine, UE4, question, CPP\
**Created:** [May 8, 2022, 4:34pm UTC](https://forums.unrealengine.com/t/setting-dynamic-material-instance-for-skeletal-static-meshes-using-c/552079 "2022-05-08T16:34:16Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [May 8, 2022, 9:26pm UTC](https://forums.unrealengine.com/t/setting-dynamic-material-instance-for-skeletal-static-meshes-using-c/552079/21 "2022-05-08T21:26:34Z")

</div>

![image_2022-05-09_022632272](https://d3kjluh73b9h9o.cloudfront.net/original/4X/b/e/9/be99c93790d21138a8cbecd5c24ab4034f5bbd52.png)

---

<div class="post-metadata">

**Author:** ![pezzott1](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/pezzott1/32/1215068_2.png) [@pezzott1](https://forums.unrealengine.com/u/pezzott1)\
**Post date:** [May 8, 2022, 9:26pm UTC](https://forums.unrealengine.com/t/setting-dynamic-material-instance-for-skeletal-static-meshes-using-c/552079/22 "2022-05-08T21:26:35Z")

</div>

Could you please paste your updated code?

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [May 8, 2022, 9:27pm UTC](https://forums.unrealengine.com/t/setting-dynamic-material-instance-for-skeletal-static-meshes-using-c/552079/23 "2022-05-08T21:27:12Z")

</div>

```auto
	static ConstructorHelpers::FObjectFinder<UMaterialInterface> MaterialSource(TEXT("/Game/Assets/Characters/FCharacter/Char_01/DynMaterials/DM_FH_01"));
	ReturnDynamicMaterial = UMaterialInstanceDynamic::Create(MaterialSource.Object, this);
	//if (MaterialSource.Object == nullptr) return;
	//{
	//	GetMesh()->SetMaterialByName("F_Head_01_A", ReturnDynamicMaterial);
	//}

	if (MaterialSource.Succeeded())
	{
		int MatIndex = GetMesh()->GetMaterialIndex("F_Head_01_A");
		GetMesh()->SetMaterial(MatIndex, ReturnDynamicMaterial);
	}

```

---

<div class="post-metadata">

**Author:** ![mnelenpridumivat](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/mnelenpridumivat/32/1194643_2.png) [@mnelenpridumivat](https://forums.unrealengine.com/u/mnelenpridumivat)\
**Post date:** [May 8, 2022, 9:32pm UTC](https://forums.unrealengine.com/t/setting-dynamic-material-instance-for-skeletal-static-meshes-using-c/552079/24 "2022-05-08T21:32:58Z")

</div>

I googled a bit, try to change `ReturnDynamicMaterial = UMaterialInstanceDynamic::Create(MaterialSource.Object, this);` to `ReturnDynamicMaterial = UMaterialInstanceDynamic::Create(MaterialSource.Object, GetWorld());` (change second parameter, maybe `InOuter` parameter means world context, not actor).

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [May 8, 2022, 9:35pm UTC](https://forums.unrealengine.com/t/setting-dynamic-material-instance-for-skeletal-static-meshes-using-c/552079/25 "2022-05-08T21:35:53Z")

</div>

![image_2022-05-09_023526703](https://d3kjluh73b9h9o.cloudfront.net/original/4X/5/1/8/51871a6a97c9522be2b77cad86fc1a878c094a69.png)  
it runs compiled successfully, but the material is not effecting the head

---

<div class="post-metadata">

**Author:** ![pezzott1](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/pezzott1/32/1215068_2.png) [@pezzott1](https://forums.unrealengine.com/u/pezzott1)\
**Post date:** [May 8, 2022, 9:38pm UTC](https://forums.unrealengine.com/t/setting-dynamic-material-instance-for-skeletal-static-meshes-using-c/552079/26 "2022-05-08T21:38:51Z")

</div>

I suggest you leave it as is was, comment every line, and start testing one by one compiling until you get to the crash.

This _is_ working for me:

```auto
void ADynamicMatTest::SetDynamicMat()
{
	static ConstructorHelpers::FObjectFinder<UMaterialInterface>Material(
		TEXT("/Game/Blueprints/Misc/M_ToCreateInstance.M_ToCreateInstance")
	);
	UMaterialInstanceDynamic* DynamicMaterial = UMaterialInstanceDynamic::Create(
		Material.Object, 
		this,
		FName("M_CreatedInstance")
	);

	if (Material.Succeeded())
	{
		StaticMesh->SetMaterial(0, DynamicMaterial);
	}
}

```

Your problem could easily be a bad path to the material.

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [May 8, 2022, 9:49pm UTC](https://forums.unrealengine.com/t/setting-dynamic-material-instance-for-skeletal-static-meshes-using-c/552079/27 "2022-05-08T21:49:36Z")

</div>

This works fine, but it deletes the base material didn’t override it, But the main thing is we found an internal engine bug, we can’t set material by name using C++, and setting it by blueprint working fine as I shown in the image.

Thank You very mush for helping and Solving this Issue. **Sir thank You very very much.** I was struggling with this from 3 days.  
The above solutions from [mnelenpridumivat](https://forums.unrealengine.com/u/mnelenpridumivat) should also work logically but because of the engine bug they don’t work.

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [May 8, 2022, 9:53pm UTC](https://forums.unrealengine.com/t/setting-dynamic-material-instance-for-skeletal-static-meshes-using-c/552079/28 "2022-05-08T21:53:34Z")

</div>

![image_2022-05-09_025314919](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/d/8/ed8aa755122ad2216ef499f93d36be6aea8d0221.png)  
Perfectly working.

---

<div class="post-metadata">

**Author:** ![pezzott1](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/pezzott1/32/1215068_2.png) [@pezzott1](https://forums.unrealengine.com/u/pezzott1)\
**Post date:** [May 9, 2022, 12:13am UTC](https://forums.unrealengine.com/t/setting-dynamic-material-instance-for-skeletal-static-meshes-using-c/552079/29 "2022-05-09T00:13:01Z")

</div>

ik it’s marked as solved, just wanted to share these… they also work:

```auto
void ADynamicMatTest::GetMaterialFromPath(FString MatPath) // Use in Constructor
{
	const TCHAR* Dir = *MatPath;
	// Find existing material
	static ConstructorHelpers::FObjectFinder<UMaterial>NewMaterial(Dir);

	// Store in UMaterial* variable
	if (NewMaterial.Succeeded())
	{
		MyMaterial = NewMaterial.Object;
	}
}

void ADynamicMatTest::CreateAndApplyNewDynMatInst(
	UMaterial* Mat, 
	UStaticMeshComponent* Mesh,
	FName SlotName,
	FName DynInstName
) // Use in or after BeginPlay
{
	// Check for nullptr
	check(Mat);
	check(Mesh);

	// Create dynamic material instance
	UMaterialInstanceDynamic* DynamicMaterial = UMaterialInstanceDynamic::Create(
		Mat,
		this,
		DynInstName
	);

	// Search material slot by name
	int MatIndex = Mesh->GetMaterialIndex(SlotName); // 0

	// Check for valid slot
	if(MatIndex != -1)
	{
		// Apply material at index
		Mesh->SetMaterial(MatIndex, DynamicMaterial);
	}
	else
	{
		UE_LOG(LogTemp, Error, TEXT("SlotName not found."));
	}
}

void ADynamicMatTest::CreateAndApplyNewDynMatInst(
	UMaterial* Mat,
	UStaticMeshComponent* Mesh,
	int MatIndex,
	FName DynInstName
) // Use in or after BeginPlay
{
	// Check for nullptr
	check(Mat);
	check(Mesh);

	if (MatIndex >= Mesh->GetMaterialSlotNames().Num())
	{
		UE_LOG(LogTemp, Error, TEXT("MatIndex out of scope."));
		return;
	}

	// Create dynamic material instance
	UMaterialInstanceDynamic* DynamicMaterial = UMaterialInstanceDynamic::Create(
		Mat,
		this,
		DynInstName
	);

	// Apply material at index
	Mesh->SetMaterial(MatIndex, DynamicMaterial);
}

```

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [May 9, 2022, 12:19am UTC](https://forums.unrealengine.com/t/setting-dynamic-material-instance-for-skeletal-static-meshes-using-c/552079/30 "2022-05-09T00:19:09Z")

</div>

Sir , Thank You very much for this, I saved it in my disk in a text document, in the near time I will working on a big workflow with materials and I will post many problems , I hope You will look into them. Thank You very much for these Solutions.

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [May 12, 2022, 1:18am UTC](https://forums.unrealengine.com/t/setting-dynamic-material-instance-for-skeletal-static-meshes-using-c/552079/31 "2022-05-12T01:18:15Z")

</div>

Sir, For some reasons the materials is not working from today again, I didn’t modify the code and everything is the same as before… I don’t know what is the reason. The game compiles and logs **“the material found successfully”** but not effecting the head.

---

<div class="post-metadata">

**Author:** ![pezzott1](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/pezzott1/32/1215068_2.png) [@pezzott1](https://forums.unrealengine.com/u/pezzott1)\
**Post date:** [May 12, 2022, 1:20am UTC](https://forums.unrealengine.com/t/setting-dynamic-material-instance-for-skeletal-static-meshes-using-c/552079/32 "2022-05-12T01:20:16Z")

</div>

Could you share both .h and .cpp?

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [May 12, 2022, 1:21am UTC](https://forums.unrealengine.com/t/setting-dynamic-material-instance-for-skeletal-static-meshes-using-c/552079/33 "2022-05-12T01:21:23Z")

</div>

Sure, I will share them now in a minute

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [May 12, 2022, 1:28am UTC](https://forums.unrealengine.com/t/setting-dynamic-material-instance-for-skeletal-static-meshes-using-c/552079/34 "2022-05-12T01:28:18Z")

</div>

[MyCharacter.h](https://forums.unrealengine.com/uploads/short-url/4QgC9lGnyfuaCuMTm09mt2nmAAM.h) (12.5 KB)  
[MyCharacter.cpp](https://forums.unrealengine.com/uploads/short-url/yw8dXU2BDeGquxc7k1vU3Oftk1A.cpp) (25.3 KB)

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [May 12, 2022, 1:34am UTC](https://forums.unrealengine.com/t/setting-dynamic-material-instance-for-skeletal-static-meshes-using-c/552079/35 "2022-05-12T01:34:57Z")

</div>

The Function **InitSkeletalMesh** is responsible to set materials.

---

<div class="post-metadata">

**Author:** ![pezzott1](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/pezzott1/32/1215068_2.png) [@pezzott1](https://forums.unrealengine.com/u/pezzott1)\
**Post date:** [May 12, 2022, 2:43am UTC](https://forums.unrealengine.com/t/setting-dynamic-material-instance-for-skeletal-static-meshes-using-c/552079/36 "2022-05-12T02:43:20Z")

</div>

Don’t see anything wrong with that specifically and its not giving you any errors… my old project is also having same issue… 🤣

Found a quick solution though. You could implement this and run the function there: [AActor::OnConstruction | Unreal Engine Documentation](https://docs.unrealengine.com/4.27/en-US/API/Runtime/Engine/GameFramework/AActor/OnConstruction/)

Tested and it works.

From Actor.h:

```auto
/**
	 * Called when an instance of this class is placed (in editor) or spawned.
	 * @param	Transform The transform the actor was constructed at.
	 */
	virtual void OnConstruction(const FTransform& Transform) {}

```

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/0/c/9/0c906393bbc887a8e6682c6c7b3140b9f4a6cf89.png)

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [May 12, 2022, 1:28pm UTC](https://forums.unrealengine.com/t/setting-dynamic-material-instance-for-skeletal-static-meshes-using-c/552079/37 "2022-05-12T13:28:53Z")

</div>

and when I implement the `initskeletalmeash` using blueprint, everything works fine… the same way calling it under constructor using blueprints.  
There is some difference between the constructor of blueprints and C++.  
I think this is because the blueprint constructor is running after the editor starts and the c++ constructor is running before the editor starts. 😃

---

<div class="post-metadata">

**Author:** ![pezzott1](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/pezzott1/32/1215068_2.png) [@pezzott1](https://forums.unrealengine.com/u/pezzott1)\
**Post date:** [May 12, 2022, 1:30pm UTC](https://forums.unrealengine.com/t/setting-dynamic-material-instance-for-skeletal-static-meshes-using-c/552079/38 "2022-05-12T13:30:14Z")

</div>

> [@Alexa.Ki](#):
>
> I think this is because the blueprint constructor is running after the editor starts

This 👆.

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [May 12, 2022, 1:32pm UTC](https://forums.unrealengine.com/t/setting-dynamic-material-instance-for-skeletal-static-meshes-using-c/552079/39 "2022-05-12T13:32:48Z")

</div>

I tried to make the function blueprintcallable to call it under blueprint constructor, but the editor crashed on this approach.

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [May 12, 2022, 5:38pm UTC](https://forums.unrealengine.com/t/setting-dynamic-material-instance-for-skeletal-static-meshes-using-c/552079/40 "2022-05-12T17:38:55Z")

</div>

> [@pezzott1](#):
>
> `M_CreatedInstance`

Changing` this` to `NULL` `(Material_H.Object, NULL, FName("Material_Instance"));` works and I have no Idea what is going on but I am sure at some point it will also stop working as before you know the situation … already my head is burning ☹ … looking like the situation is forcing me to build the game with blueprints…

[Previous page](https://forums.unrealengine.com/t/setting-dynamic-material-instance-for-skeletal-static-meshes-using-c/552079.md?page=1)
