# How to set Dynamic material Instance in C++?

**URL:** <https://forums.unrealengine.com/t/how-to-set-dynamic-material-instance-in-c/551257>\
**Category:** C++\
**Tags:** UE4-20, unreal-engine, UE4, CPP, question\
**Created:** [May 7, 2022, 1:10pm UTC](https://forums.unrealengine.com/t/how-to-set-dynamic-material-instance-in-c/551257 "2022-05-07T13:10:59Z")\
**Posts on this page:** 12\
**Page:** 1

<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 7, 2022, 1:11pm UTC](https://forums.unrealengine.com/t/how-to-set-dynamic-material-instance-in-c/551257/1 "2022-05-07T13:11:00Z")

</div>

I am trying to set Dynamic material instance in C++ , the example is provided in blueprint script image:

 ![image_2022-05-07_180926299](https://d3kjluh73b9h9o.cloudfront.net/original/4X/d/5/1/d51f3076869cdde19119200966a624f0737e9bab.png)

How to do this properly and if possible I want to do it on a very cheap way so it will not effect the performance.  
Thank You

---

<div class="post-metadata">

**Author:** ![Tuerer](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/tuerer/32/1008648_2.png) [@Tuerer](https://forums.unrealengine.com/u/Tuerer)\
**Post date:** [May 7, 2022, 1:23pm UTC](https://forums.unrealengine.com/t/how-to-set-dynamic-material-instance-in-c/551257/2 "2022-05-07T13:23:57Z")

</div>

[Creating a dynamic material instance on C++? - Unreal Engine / Asset Creation - Unreal Engine Forums](https://forums.unrealengine.com/t/creating-a-dynamic-material-instance-on-c/362062)

---

<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 7, 2022, 1:41pm UTC](https://forums.unrealengine.com/t/how-to-set-dynamic-material-instance-in-c/551257/3 "2022-05-07T13:41:54Z")

</div>

Sir I am unable to understand this solution and don’t get any idea from it to implement my own function.

---

<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 7, 2022, 1:43pm UTC](https://forums.unrealengine.com/t/how-to-set-dynamic-material-instance-in-c/551257/4 "2022-05-07T13:43:39Z")

</div>

Their is nothing about material slot name, material name, and creation flags information.

---

<div class="post-metadata">

**Author:** ![c0r37py](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/c0r37py/32/559700_2.png) [@c0r37py](https://forums.unrealengine.com/u/c0r37py)\
**Post date:** [May 7, 2022, 2:47pm UTC](https://forums.unrealengine.com/t/how-to-set-dynamic-material-instance-in-c/551257/5 "2022-05-07T14:47:11Z")

</div>

```auto
void AMCharacter:Myfunc()
{
	UMaterialInstanceDynamic* MaterialRef = UMaterialInstanceDynamic::Create(Material, this);
	if (MaterialRef != nullptr)
	{
		auto Material = ConstructorHelpers::FObjectFinder<UMaterial>(TEXT("/Game/material location"));
		MaterialRef = GetMesh()->CreateDynamicMaterialInstance(0, Material.Object);
		if (Material.Object != nullptr)
		{
			GetMesh()->SetMaterialByName("material slot name", MaterialRef);
		}
		return;
	}
	return;

```

---

<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 7, 2022, 2:50pm UTC](https://forums.unrealengine.com/t/how-to-set-dynamic-material-instance-in-c/551257/6 "2022-05-07T14:50:21Z")

</div>

Sir, material is not successfully applied to the mesh, I am trying to apply on a skeletal mesh.

---

<div class="post-metadata">

**Author:** ![c0r37py](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/c0r37py/32/559700_2.png) [@c0r37py](https://forums.unrealengine.com/u/c0r37py)\
**Post date:** [May 7, 2022, 2:54pm UTC](https://forums.unrealengine.com/t/how-to-set-dynamic-material-instance-in-c/551257/7 "2022-05-07T14:54:37Z")

</div>

On your mesh you have a slot name, set this in the 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 7, 2022, 2:57pm UTC](https://forums.unrealengine.com/t/how-to-set-dynamic-material-instance-in-c/551257/8 "2022-05-07T14:57:03Z")

</div>

Still not working ☹

---

<div class="post-metadata">

**Author:** ![c0r37py](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/c0r37py/32/559700_2.png) [@c0r37py](https://forums.unrealengine.com/u/c0r37py)\
**Post date:** [May 7, 2022, 3:05pm UTC](https://forums.unrealengine.com/t/how-to-set-dynamic-material-instance-in-c/551257/9 "2022-05-07T15:05:16Z")

</div>

> [@Setting Material and Material Instance In c++ Without Crashing](https://forums.unrealengine.com/t/setting-material-and-material-instance-in-c-without-crashing/384303):
>
> I’m in the process of converting some of my blueprints to code and I keep hitting this problem. Maybe I’m missing something obvious but it causes the engine to crash when the code should be changing the material. this is what I’m trying to achieve [125411-temp.jpg] .cpp .h [125399-temp.jpg] and this is the error i get from it

---

<div class="post-metadata">

**Author:** ![Tuerer](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/tuerer/32/1008648_2.png) [@Tuerer](https://forums.unrealengine.com/u/Tuerer)\
**Post date:** [May 7, 2022, 3:20pm UTC](https://forums.unrealengine.com/t/how-to-set-dynamic-material-instance-in-c/551257/10 "2022-05-07T15:20:58Z")

</div>

Okay, I see you’re asking quite a lot of C++ questions here. Community’s help is nice and all, but you need to be able to find ways to do stuff on your own using the Engine code available to you.

Look at the nodes you have. SetMaterialByName states that Target is PrimitiveComponent. Just open _PrimitiveComponent.h_ and/or _PrimitiveComponent.cpp_ in your IDE and look for the function in question. You can find `SetMaterialByName()` there, and BTW there’s another `CreateDynamicMaterialInstance()` function that creates a dynamic material in another way.

Now, there can be some classes you’re not familiar with, e.g. _UMaterialInterface_ and _UMaterialInstanceDynamic_. But if you just google them, you’ll easily find them in the official documentation (the engine prefixes make it really easy to find classes you need). On the documentation page, the most important things to look for are _Include_, because oftentimes you won’t be able to compile without it, and _Module_, because certain modules are not enabled by default, but in this case you’re fine, that’s a topic of another discussion.

Now, in _MaterialInstanceDynamic.h_ you can find several `Create()` functions, and note that they are _static_; (BTW, once again, you can consult _KismetMaterialLibrary.cpp_ to see what functions it uses. To know that you need to look there, you can hover the _CreateMaterialInstanceDynamic_ node and see what its target is).

Now, when you know what to include to make it work, you can:

1. Declare your parent material and your material instance in .h:

```auto
class UMaterialInterface* ParentMaterial;
class UMaterialInstanceDynamic* DynamicMaterial;

```

If you’re going to use them in the constructor, or if you want to set ParentMaterial in bp, and not through ConstructorHelpers, make sure to mark them as UPROPERTY() with respective flags;

1. In .cpp, include those things you found in the documentation. Now you can use the pointers you have declared. Just don’t forget to set reference to the parent material, or you’ll get a crash.

Remember those _static_ functions? _Static_ means that you can call them like this:

```auto
DynamicMaterial = UMaterialInstanceDynamic::Create(ParentMaterial, this, FName("MyDynMat"));

Mesh->SetMaterialByName(FName("MySlotName"), DynamicMaterial);

```

As for `UObject* InOuter`, I’m not entirely sure what that is, but _“this”_ works, and I’m fine with that.

Anyway, that’s my workflow when it comes to something unfamiliar. Sure, you can ask the community and hope someone will provide you the answer, but if you have undertaken C++, you need to be able to analyze the engine code yourself. It’ll pay out in future.

Hope this helps.

---

<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 7, 2022, 3:35pm UTC](https://forums.unrealengine.com/t/how-to-set-dynamic-material-instance-in-c/551257/12 "2022-05-07T15:35:52Z")

</div>

> [@Alexa.Ki](#):
>
> Thank You very very much Sir for solving my problem and helping in my studies, I have learned lots of stuff from the community and very thankful for this.
> 
> I am learning analyzing the engine code, but its a little hard for me the limited syntax examples on the official documentation .

Thank You very very much Sir for solving my problem and helping in my studies, I have learned lots of stuff from the community and very thankful for this.

I am learning analyzing the engine code, but its a little hard for me the limited syntax examples on the official documentation .

---

<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 7, 2022, 3:36pm UTC](https://forums.unrealengine.com/t/how-to-set-dynamic-material-instance-in-c/551257/13 "2022-05-07T15:36:27Z")

</div>

Thank You very very much Sir for solving my problem and helping in my studies, I have learned lots of stuff from the community and very thankful for this.

I am learning analyzing the engine code, but its a little hard for me the limited syntax examples on the official documentation .
