# Why the Dynamic material is always returning NULL in switch case?

**URL:** <https://forums.unrealengine.com/t/why-the-dynamic-material-is-always-returning-null-in-switch-case/559514>\
**Category:** C++\
**Tags:** UE4-20, unreal-engine, UE4, question, CPP\
**Created:** [May 17, 2022, 4:57pm UTC](https://forums.unrealengine.com/t/why-the-dynamic-material-is-always-returning-null-in-switch-case/559514 "2022-05-17T16:57:36Z")\
**Posts on this page:** 9\
**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 17, 2022, 4:57pm UTC](https://forums.unrealengine.com/t/why-the-dynamic-material-is-always-returning-null-in-switch-case/559514/1 "2022-05-17T16:57:36Z")

</div>

Without wasting your time, I am explaining quickly the problem.  
1: I have the texture in the data table.  
2: I want to grab that texture and set it to the **Param2D** in the material with the parameter texture name is **MaskShirt2D**  
3: Next I will post the code currently I am trying to achieve this.

**Struct**

 ![02](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/b/0/7b004d41f61749db5d253443b1ff4c66d57f2baa.png)  
**Data Table**  
 ![01](https://d3kjluh73b9h9o.cloudfront.net/original/4X/1/9/0/1901e8c2f0553de44c10121732d06d3617696de3.png)  
**Dynamic Material:** Material\_BodyRef is not null here.  
 ![05](https://d3kjluh73b9h9o.cloudfront.net/original/4X/a/5/9/a5925a8aa6f90af29a91c87fa814becff2d32827.png)  
**Getting Datas from Data table by ID**  
 ![image_2022-05-17_214611473](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/3/3/e3325b5a22dccef3390215c32673461665a9dfdf.png)

**Enum**  
 ![03](https://d3kjluh73b9h9o.cloudfront.net/original/4X/b/c/f/bcfe9a1a1ac22029916370c878dc387de959d785.png)

**Switch:** Material\_BodyRef is returning null here.

 ![04](https://d3kjluh73b9h9o.cloudfront.net/original/4X/3/b/5/3b5bf2348712b8bfecb04ae1b76522b8d03bb76c.png)

Note: Doing this without changing the texture parameters works fine. The problem is the **Material\_BodyRef** is null only in this switch case statement,

 ![image_2022-05-17_215251411](https://d3kjluh73b9h9o.cloudfront.net/original/4X/b/b/2/bb2f724c4f5b5e4bcc49cd7a1e69e3f8269c6416.png)

**Blueprint Example of what I want to achieve.** Doing this with blueprints works fine.

 ![06](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/9/1/691303dede5c879f01fcbbe7dc6b2d35a97e12a9.png)

Thank You in advance for helping in my study progress. If You need any additional information just let me know.

---

<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 17, 2022, 5:21pm UTC](https://forums.unrealengine.com/t/why-the-dynamic-material-is-always-returning-null-in-switch-case/559514/2 "2022-05-17T17:21:33Z")

</div>

Hi Alexa, are you setting the dynamic material in the default constructor or OnConstruction?  
Please provide this information I will try to solve, Thanks

---

<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 17, 2022, 5:23pm UTC](https://forums.unrealengine.com/t/why-the-dynamic-material-is-always-returning-null-in-switch-case/559514/3 "2022-05-17T17:23:31Z")

</div>

Hello, I have a seperate function for this `void AMyharacter::InitSkeletalMesh();` and I am calling this function in the default construction once.

---

<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 17, 2022, 5:24pm UTC](https://forums.unrealengine.com/t/why-the-dynamic-material-is-always-returning-null-in-switch-case/559514/4 "2022-05-17T17:24:59Z")

</div>

your dynamic material will always be nulled while it calls only once on runtime, you have to call **InitSkeletalMesh();** under OnConstruciton or call it after you check for case.

---

<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 17, 2022, 5:25pm UTC](https://forums.unrealengine.com/t/why-the-dynamic-material-is-always-returning-null-in-switch-case/559514/5 "2022-05-17T17:25:45Z")

</div>

How? Please give me a small example. Thank You Sir for Help

---

<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 17, 2022, 5:29pm UTC](https://forums.unrealengine.com/t/why-the-dynamic-material-is-always-returning-null-in-switch-case/559514/6 "2022-05-17T17:29:21Z")

</div>

void APUBGCharacter::OnConstruction(const FTransform& Transform)  
{ //Call Super and then InitSkeletalMesh();}

or

```auto

case EFashionType::E_Jacket:
{
			InitSkeletalMesh(); //Calling this here will always return **Material_BodyRef** `!null`
			if (::IsValid(Material_BodyRef))
            {//Logic}
}

```

cheers.

---

<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 17, 2022, 5:31pm UTC](https://forums.unrealengine.com/t/why-the-dynamic-material-is-always-returning-null-in-switch-case/559514/7 "2022-05-17T17:31:46Z")

</div>

Working like a Charm 😃 )  
Thank You Sir for Help, really appreciated and I actually learned something big , Thank U for helping me in my Studies.

---

<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 17, 2022, 5:38pm UTC](https://forums.unrealengine.com/t/why-the-dynamic-material-is-always-returning-null-in-switch-case/559514/8 "2022-05-17T17:38:07Z")

</div>

Calling that function only when it need to be called will save performance, don’t call in tick , you don’t need that to be called every second, only if in `EFashionType::E_Jacket:` you wil need to call it and in other conditions as I can see in you **Enum** , you have other cases too.  
cheers.

---

<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 17, 2022, 5:39pm UTC](https://forums.unrealengine.com/t/why-the-dynamic-material-is-always-returning-null-in-switch-case/559514/9 "2022-05-17T17:39:33Z")

</div>

Yes right, I got that Idea, Thank You for the help )
