How to lerp material color in C++ ?

I created a Blueprint from a C++ class. In that Blueprint, I added a static mesh component (a plane). This plane uses a simple material, only a color.

I want to be able to change the color of this plane, progressively in the C++ class.

I investigated two strategies: Creating 2 materials and switching from one to the other, or creating a material with 2 colors, and using a lerp function. The second strategy seems more accessible, but I still need help on that.

So here is how I set up the material: 2 colors, 1 lerp function to blend the colors, and a scalar parameter that I called “Blend” to input the proportion of each color in the blend.

In my C++ class I can access to the material with this code:

auto Plane = FindComponentByClass<UStaticMeshComponent>();
auto material = Plane->GetMaterial(0);

Then, what I would like to do in the C++ class is something like

SetScalarParameterValue(TEXT("Blend"), value);

But there is no such function… There is only a


Any idea how I could modify the scalar parameter from the C++ class?
I would also be interested in another way to change the color from the C++ class if there is better strategy.

Following Roel advices, I created a MaterialInstanceDynamic, and called SetScalarParameterValue() on this MaterialInstanceDynamic.

In the .f file I added:

UMaterialInstanceDynamic* DynamicMaterial;

In the .cc file I added:

void AMyPlane::BeginPlay()

	auto Plane = FindComponentByClass<UStaticMeshComponent>();
	auto material = Plane->GetMaterial(0);
	DynamicMaterial = UMaterialInstanceDynamic::Create(material, NULL);
	Plane->SetMaterial(0, DynamicMaterial);

void AMyPlanel::Tick(float DeltaTime)
    // Testing the color switch at runtime
	float blend = 0.5f + FMath::Cos(GetWorld()->TimeSeconds)/2;
	DynamicMaterial->SetScalarParameterValue(TEXT("Blend"), blend);



Only a material instance dynamic has the functionality of setting parameters at run time.
So the first step would to to create a material instance dynamic from the required material and apply it to a the primitive component. Luckily, Epic did the work for you and included a function in the primitive component class.
Any primitive component like your static mesh component can call this function:
virtual class UMaterialInstanceDynamic* CreateDynamicMaterialInstance(int32 ElementIndex, class UMaterialInterface* SourceMaterial = NULL, FName OptionalName = NAME_None);

Once you have the material instance dynamic you can call the set ‘x’ parameter value functions on it.

Have you tried with UMeshComponent::SetScalarParameterValueOnMaterials? you can call that on your Plane, or there is UKismetMaterialLibrary::SetScalarParameterValue but I don’t really know what that @param: UMaterialParameterCollection is or how you get that.

Only downside of that method on the UMeshComponent would be that if you have the same parameter on more than one material on the mesh looks like it will change the scalar as well, so be careful.

Hope this helps.

Maybe it´s just enough to replace the auto and cast the return to a UMaterial, because the GetMaterial returns a UMaterialInterface.
So perhabs try something like this:

UMaterial* material = Cast<UMaterial>(plane->GetMaterial(0));

Thank you! Material instance dynamic was exactly what I needed. I updated my question with the solution.