How to set the color of a UMaterialInstanceDynamic?

In spite of it’s name, I cannot seem to set the colour of a dynamic material instance. Here is what I’m trying to do to accomplish this in the constructor for my actor class:

MeshComponent = CreateDefaultSubobject< UStaticMeshComponent >(
                                            TEXT("MeshComponent") );
MeshComponent->SetMobility( EComponentMobility::Movable );
MeshComponent->SetStaticMesh( Mesh );    // Mesh is a UStaticMesh asset found earlier

UMaterialInstanceDynamic * DynamicMaterial =
            MeshComponent->CreateAndSetMaterialInstanceDynamic( 0 );

// I want my colour to be emissive, but setting either of these has no effect.
DynamicMaterial->SetVectorParameterValue( FName("Color"),
                                FLinearColor( 1.0f, 0.0f, 0.0f ) );
DynamicMaterial->SetVectorParameterValue( FName("Emissive Color"),
                                FLinearColor( 1.0f, 0.0f, 0.0f ) );

// For good measure I tried setting it again, but no effect.
MeshComponent->SetMaterial( 0, DynamicMaterial );

I’ve tried creating the dynamic material with a parent material but then only the parent material’s properties are ever used. I just can’t seem to get any parameter setting to take affect. Any ideas on how to do this correctly? or if this is even possible?

1 Like

OK, I finally figured out how to do this. The key part was using a material I’ve created with the appropriate parameter definitions as the parent material. Otherwise, the parameter settings have no effect since nothing usese them. For example, if you have an FLinearColor or something similar connected to the “Color” or “Emissive Color” node of your base material, then you right click the connector on the linearcolor and select “add parameter”. Then, when you set that parameter, it’s like that input changes! Here’s my code when this worked:

MeshComponent = CreateDefaultSubobject< UStaticMeshComponent >(
                                            TEXT("MeshComponent") );
MeshComponent->SetMobility( EComponentMobility::Movable );
MeshComponent->SetStaticMesh( Mesh );

// WhiteMaterial is a material asset found earlier, with a paramterized connection to "Emissive Color"
UMaterialInstanceDynamic * DynamicMaterial =
            UMaterialInstanceDynamic::Create( WhiteMaterial, NULL );
DynamicMaterial->SetVectorParameterValue( FName("Color"),
                                FLinearColor( 100.0f, 0.0f, 0.0f ) );
MeshComponent->SetMaterial( 0, DynamicMaterial );
2 Likes

I know its a little late but can you expound on this one? Did you do some changes to the blueprint? I am having the same trouble with my material instance right now.