Dymanic Material SetVectorParameterValue Crash

I have a post process Material as UMaterial* PPOutline1 and a DynamicMaterial as a UMaterialInstanceDynamic in my Character.h

	UMaterial* PPOutline1;
	UMaterialInstanceDynamic* DynamicMaterial;

What I want to is change a Vector Paramter in PPOutline to different colors based on health. I know most of the logic but I can’t seem to get Dyanmic Materials working.

In Character.cpp the constructor I do

	static ConstructorHelpers::FObjectFinder<UMaterial> PPFinder(TEXT("/Game/Environment/PP_OutlineCustomDepthOcclusion"));
	if (PPFinder.Succeeded()){
		PPOutline1 = (UMaterial*) PPFinder.Object;
		DynamicMaterial = (UMaterialInstanceDynamic*) PPOutline1;
	}

And then later when it comes time to set the new color for the VectorParam of the Dynamic Material I do this

				if (OtherCharacter->Health / OtherCharacter->GetMaxHealth() > 0.9f){
					UE_LOG(LogTemp, Warning, TEXT("Green Health"));
					if (DynamicMaterial != NULL){
						DynamicMaterial->SetVectorParameterValue(FName("OutlineColor"), FLinearColor(0.0f, 1.0f, 0.0f, 1.0f));
						   						   						
					}
}

However I always get the following crash and I know its on the line

DynamicMaterial->SetVectorParameterValue(FName("OutlineColor"), FLinearColor(0.0f, 1.0f, 0.0f, 1.0f));

http://puu.sh/iYrvr/28b360287e.png
http://puu.sh/iYrw2/76423cfd2c.png

Hi i had allways some issues with dynamic materials :slight_smile:

But steps that fix allmost all of my problems are:

  1. Making checks by using IsValidLowLevel() on material instance
  2. Defining variable as UPROPERTY

Hope it can help u :slight_smile:

1 Like

Hy Ypsylon,

Thanks heaps :slight_smile:

Putting the UPROPERTY macro back in front of my dynamic material in my code fixed it!

1 Like

Thanks a lot, still a thing in 2021

1 Like