How do I change a material color at runtime?

I ma making an atmosphere simulation that has a grid of cubes for visual representation. I want the material on each cube static mesh to change color according to a float variable. I can create this behavior except for one problem, I can’t figure out how to change a material’s base color in C++.

I have tried the examples below and it doesn’t work.

UMaterialInterface* mat = cube->GetMaterial(0);
UMaterialInstanceDynamic* dMat = CreateDynamicMaterialInstance(0, mat);
dMat->SetVectorParameterValue("color", FLinearColor::Red);

I don’t need help with the color change code I just need to know how to access the material’s color.
Thanks to anyone that helps.

You need to have a parameter in the material you are trying to edit with the same name as the one you are trying to pass into that function, in your case a vector parameter with the name “color”.

Are the parameters already on the material or do I need to make one? Also what about when it says the SetVectorParameterValue() is in accessible, is that just a intellisense error?

you have to make it in the material editor.
to do so right click on the constant vector node that your are using for color and select “convert to parameter” name it “color”.

No, you need to setup one yourself, because you can use vector parameters for almost everything in the material editor (color is just a very basic example of using it).

As for the inaccessible, that may be just an intellisense error. As long as it compiles, there should be no problem. I use the SetVectorParameter function a lot myself and never had any problems with it thus far.

Thank you both for the help. I was able to figure it out because of you.