How to cast MaterialInstanceConstant

FString matPath = “Material’/Game/StarterContent/Materials/M_Metal_Gold.M_Metal_Gold’”;
UMaterialInstanceConstant* material = Cast(StaticLoadObject(UMaterialInstanceConstant::StaticClass(), nullptr, *(matPath)));
if (material == NULL) {
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT(“Material not found”));
}

This code fail to create UMaterialInstanceConstant.
I do not know why.

The code failed because your asset is a UMaterial, not a UMaterialInstanceConstant. If your code doesn’t care about the material’s implementation details, I’d recommend instead casting it to UMaterialInterface which would correctly work with any material, instanced or not.

Thanks to answer my question! I will try out and give a feedback to you.

Now I can change actor’s material dynamically!

Great! Please mark the answer as accepted so I get karma and the next person can find it.