How can I set a scalar parameter on a material for a static mesh?

I’ve looked at other questions pertaining to this topic, but they all seem to revolve around FObjectFinder in the constructor. Both my dynamic material and static mesh are being set in blueprints. I need to get that material off of my static mesh component with C++. This is how it’s done in blueprints:

I just need to know what is the C++ equivalent of the above? I’ve tried:


However, I can’t reach the material from there unless I’m missing something. I could really use some help.


 if(!TheStaticMeshActor || !TheStaticMeshActor->StaticMeshComponent)

    UMaterialInstanceDynamic* Mat_Inst = TheStaticMeshActor->StaticMeshComponent->GetMaterial(0);
      Mat_Inst->SetScalarParameterValue(FName("ParameterName"), 333.014); //new value


for more info



Thanks, but how can I actually see the StaticMeshActor and Component? I have a reference to my actor, but I’m not sure how to get the component. I’ve tried FindComponentByClass and GetComponentByClass like this:

C->FindComponentByClass(UStaticMeshComponent::StaticClass()) but I can’t seem to get the actual component.

C is my actor.