If I change a Material's parameter value, all of my objects changes their colour

I have a material with a Vector Parameter Value to allow me to change its colour. I have also created a Material Instance for this material.

I’m trying to create many actors with this material instance but with different colours but I can’t. I don’t this programmatically. The code is:

At class constructor:

static ConstructorHelpers::FObjectFinder<UMaterialInstance> MatFinder(TEXT("MaterialInstanceConstant'/Game/Geometry/Materials/Star_Inst.Star_Inst'")); // 'Material /Game/Geometry/Materials/Star.Star'
if (MatFinder.Succeeded())
{
	StarMaterialInstance = MatFinder.Object;
}

And in PostInitialiteMethod:

void AStarActor::PostInitializeComponents()
{
	Super::PostInitializeComponents();

	UMaterialInstanceDynamic* matInstance = UMaterialInstanceDynamic::Create(StarMaterialInstance, this);

	matInstance->SetVectorParameterValue(FName(TEXT("BaseColor")), FLinearColor(255.0, 255.0, 255.0, 1.0));
	matInstance->SetVectorParameterValue(FName(TEXT("EmiterColor")), FLinearColor(125.0, 125.0, 125.0, 1.0));
	matInstance->SetScalarParameterValue(FName(TEXT("Magnitude")), -1);
	SphereVisual->SetMaterial(0, matInstance);
}

But, all the instances have the same colour!!! I have two meshes in the editor, and they also change their colour.

How create many AStarActor instances with different colours using the same Material Instance?

This is my material setup:

You are setting the same color in PostInitializeComponents, that’s why they all have the same color. Try changing the color.
You can generate random color for ex. generate random vector with FMath::VRand and create new FLinearColor using vector values.

 matInstance->SetVectorParameterValue(FName(TEXT("BaseColor")), FLinearColor(FMath::VRand()));
 matInstance->SetVectorParameterValue(FName(TEXT("EmiterColor")), FLinearColor(FMath::VRand()));

will work I think.

I have put two instances of my class in Unreal Engine with a different colour and when I play the game these two instances change their colour.

I am not sure, but I think you should get the reference to a material, not a material instance. And then create dynamic material instance from it.
Static Material Instances are for setting material parameters at design time for use in the editor, i think.

Thanks, but I have tried what you have suggested me and I get the same result. Maybe, I’m using the same Material with all of them and this is why I change the colour in all of them.

It is strange. Can you post you material setup?

I don’t know what do you mean with material setup, but I have updated my question with a picture.

I have created this git repository if you want to check all the project: https://github.com/ViaCognita/Estrellas
You can also download Estrellas.sdf 7zipped here: Microsoft OneDrive - Access files anywhere. Create docs with free Office Online.

Yes, it would be very handy, but please add all the files. )

Hi, ViaCognita

Here what i got

And here is the code for my actors http://pastebin.com/4sx68Tn2
Setting parameters works ok. you can compare it to yours.
What is interesting about your material is some color math which involves multiplication. Check it if it is ok.

Sorry, I have forgotten to push it to Github. You can also download Estrellas.sdf 7zipped here: Microsoft OneDrive - Access files anywhere. Create docs with free Office Online.

Thanks a lot. Your solution works perfectly. Now I have understood the problem.