Change vector parameter value of instance material

Hi!

I’m using Unreal 5.4.2.

I’m using the SM_Cube_01 Static Mesh from the VR Template content in an Actor.

With the material instance MI_Cube_01:

I want to change the Value of the GlowColor with this code:

TObjectPtr<UMaterialInstanceConstant> Mat_Inst = Cast<UMaterialInstanceConstant>(StaticMesh->GetMaterial(0));
if (Mat_Inst)
{
	FMaterialParameterInfo ParameterInfo(FName("GlowColor"));
	Mat_Inst->SetVectorParameterValueEditorOnly(ParameterInfo, CubeColour);
}

I have many actors on the level that uses this Static Mesh. When I change the parameter value, it changes in all of the actors in the level.

How can I change this parameter value?

I’ve used this node in Blueprint and it works perfectly:

That’s the proper way to set the parameter. The think you are missing is creating a runtime material instance on the component. That way the material will only change on that particular instance of the mesh.

Example of creating a material instance from our code:

UMaterialInstanceDynamic* matInstance = primitiveComponent->CreateAndSetMaterialInstanceDynamic(materialIndex);

I don’t use blueprints but something like this tutorial should work:

UE5 Blueprint Tutorial - How to Create Dynamic Material Instances - YouTube