# How to change material for a mesh in cpp?

**URL:** <https://forums.unrealengine.com/t/how-to-change-material-for-a-mesh-in-cpp/613186>\
**Category:** C++\
**Tags:** unreal-engine, UE4, CPP, question\
**Created:** [July 24, 2022, 3:51pm UTC](https://forums.unrealengine.com/t/how-to-change-material-for-a-mesh-in-cpp/613186 "2022-07-24T15:51:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![PsyKuKiller](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@PsyKuKiller](https://forums.unrealengine.com/u/PsyKuKiller)\
**Post date:** [July 24, 2022, 3:51pm UTC](https://forums.unrealengine.com/t/how-to-change-material-for-a-mesh-in-cpp/613186/1 "2022-07-24T15:51:54Z")

</div>

Hi guys,

Let’s say I have the reference of a material in the editor [via right click, copy reference]. And I have a static mesh.  
Using the function SetMaterial() of the static mesh component, the function takes a UMaterialInterface pointer type as a parameter.  
How can I use the material reference copied from the editor and change the mesh’s material? I didn’t find an immediate set material function in UMaterialInterface, and I am not familiar with materials in cpp at all.

---

<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:** [July 24, 2022, 4:21pm UTC](https://forums.unrealengine.com/t/how-to-change-material-for-a-mesh-in-cpp/613186/2 "2022-07-24T16:21:33Z")

</div>

The way I generally do it is: declare the material in .h (this is in case you create blueprints based on the cpp class:

```auto
UPROPERTY(EditAnywhere)
UMaterialInterface* MaterialRef;

```

Set the reference in the blueprint, and then you can use it in your cpp.

---

<div class="post-metadata">

**Author:** ![SolidGasStudios](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/solidgasstudios/32/1194575_2.png) [@SolidGasStudios](https://forums.unrealengine.com/u/SolidGasStudios)\
**Post date:** [July 24, 2022, 4:23pm UTC](https://forums.unrealengine.com/t/how-to-change-material-for-a-mesh-in-cpp/613186/3 "2022-07-24T16:23:59Z")

</div>

A better approach it to be able to set the material in your Blueprint, rather than have a hardcoded path.

.h

```auto
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = MyBP)
		UMaterialInterface* MyMaterial;

```

.cpp

```auto
if(MyMaterial){
 MyStaticMesh->SetMaterial(0, MyMaterial);
}

```

---

<div class="post-metadata">

**Author:** ![PsyKuKiller](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@PsyKuKiller](https://forums.unrealengine.com/u/PsyKuKiller)\
**Post date:** [July 24, 2022, 4:37pm UTC](https://forums.unrealengine.com/t/how-to-change-material-for-a-mesh-in-cpp/613186/4 "2022-07-24T16:37:16Z")

</div>

Thanks, this is useful.
