I assume BoxVisual != nullptr
? (looking at the way you have written that code, I would think you have checked that).
This is something I am playing with myself, and I use CreateDynamicMaterialInstance():
static ConstructorHelpers::FObjectFinder<UMaterial> Material(TEXT("/Game/StarterContent/Materials/M_Metal_Steel"));
if (Material.Succeeded()) {
// Save the return value as an instance variable if you want to set material params later...
BoxVisual->CreateDynamicMaterialInstance(0, Material.Object);
}
This works some of the time however more often than not the material does not appear to be applied to the mesh. I have this working all of the time in a Blueprint, so I have come to the conclusion that I must use a C++ base class to provide the functionality I want (I want to set lots of different material parameters to different values depending on the value of a enum - something that’s impractical in a BP). However I will be using the BP subclass to set-up the mesh and create and save the Material Instance Dynamic (MID), as that works. It will be more flexible that way anyway.