# CreateDynamicMaterialInstance() returns nullptr

**URL:** <https://forums.unrealengine.com/t/createdynamicmaterialinstance-returns-nullptr/373279>\
**Category:** Rendering\
**Tags:** unreal-engine, CPP, question\
**Created:** [October 13, 2016, 9:52pm UTC](https://forums.unrealengine.com/t/createdynamicmaterialinstance-returns-nullptr/373279 "2016-10-13T21:52:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luis\_Martinez](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Luis\_Martinez](https://forums.unrealengine.com/u/Luis_Martinez)\
**Post date:** [October 13, 2016, 9:52pm UTC](https://forums.unrealengine.com/t/createdynamicmaterialinstance-returns-nullptr/373279/1 "2016-10-13T21:52:00Z")

</div>

Wondering why when I use

.H  
UMaterialInstanceDynamic\* NewFlagMaterial;

.CPP  
NewFlagMaterial = Mesh-\>CreateDynamicMaterialInstance(0);

NewFlagMaterial stays with a Null reference, even though I do have reference to my Mesh.

I’ve used this same setup about 3 times through my game and it works perfectly… no idea what might’ve happened.

 ![110779-screenshot+(64).png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/b/8/7b843b4c479887733d3257a6b3ea3f8468f97747.png)

 ![110780-screenshot+(65).png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/1/9/c1933a3cb54c5243ef21a33a4e6bb0209ba30ae4.png)

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_41845f4a](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_41845f4a](https://forums.unrealengine.com/u/anonymous_user_41845f4a)\
**Post date:** [October 14, 2016, 5:03pm UTC](https://forums.unrealengine.com/t/createdynamicmaterialinstance-returns-nullptr/373279/2 "2016-10-14T17:03:16Z")

</div>

Hello ,

Is it possible that the index that you’re supplying is invalid? From looking at the documentation for this function, it seems like that is the only time it returns a null.

> **[UPrimitiveComponent::CreateDynamicMaterialInstance](https://docs.unrealengine.com/5.1/en-US/API/Runtime/Engine/Components/UPrimitiveComponent/CreateDynamicMaterialInstance/)**
>
> Creates a Dynamic Material Instance for the specified element index, optionally from the supplied material.

---

<div class="post-metadata">

**Author:** ![Luis\_Martinez](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Luis\_Martinez](https://forums.unrealengine.com/u/Luis_Martinez)\
**Post date:** [October 19, 2016, 5:03am UTC](https://forums.unrealengine.com/t/createdynamicmaterialinstance-returns-nullptr/373279/3 "2016-10-19T05:03:05Z")

</div>

Sorry for the slow response, I don’t think index is the cause of it, since I use the following :

Mesh-\>SetMaterial(0, xMaterial);

and it does set whatever material I put into it.

---

<div class="post-metadata">

**Author:** ![GigasightMedia](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@GigasightMedia](https://forums.unrealengine.com/u/GigasightMedia)\
**Post date:** [October 19, 2016, 5:51am UTC](https://forums.unrealengine.com/t/createdynamicmaterialinstance-returns-nullptr/373279/4 "2016-10-19T05:51:19Z")

</div>

Try passing your source material along with you call, see if that helps:

```
void AFlag::Multicast_BlueFlagColor_Implemenation()
{
    NewFlagMaterial = Mesh->CreateDynamicMaterialInstance(0, FlagMaterials[1]);
    BlueFlagColorChange();
}

```

Since you’re setting the materials immediately before replacing it, this is probably a better way to go anyway.

Or maybe try:

```
void AFlag::Multicast_RedFlagColor_Implemenation()
{
    NewFlagMaterial = Mesh->CreateAndSetMaterialInstanceDynamicFromMaterial(0, FlagMaterials[2]);
    RedFlagColorChange();
}

```

Or you could try:

```
void AFlag::Multicast_RedFlagColor_Implemenation()
{
    NewFlagMaterial = UKismetMaterialLibrary::CreateDynamicMaterialInstance(this, FlagMaterials[2]);
    Mesh->SetMaterial(0, NewFlagMaterial);
    RedFlagColorChange();
}
```

---

<div class="post-metadata">

**Author:** ![Luis\_Martinez](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Luis\_Martinez](https://forums.unrealengine.com/u/Luis_Martinez)\
**Post date:** [October 19, 2016, 6:13pm UTC](https://forums.unrealengine.com/t/createdynamicmaterialinstance-returns-nullptr/373279/5 "2016-10-19T18:13:50Z")

</div>

Thank you for the information, I could’ve sworn I tried both of this methods out, but it appears I wasn’t placing something right. It is now returning a valid value, still not working in the Set Scalar parameter value through the timeline, but in this case it would have to be something else.

Thanks !
