SetVectorParameterValue crashing the editor during compilation


I’m trying to spawn decals under my characters as team markers. The decals are correctly spawned with C++ using default values, however the compilation instantly crashes the editor when I attempt to modify the VectorParameterValue (same with scalar parameters.)

As for my code :


	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = CharacterTeamMarker)
		bool bCharacterTeam1;

	class UMaterialInstanceDynamic* TeamMarkerDynamic;

	FLinearColor TeamMarkerColor;


ACharacterUnit::ACharacterUnit() {
	static ConstructorHelpers::FObjectFinder<UMaterial> TeamMarkerMaterial(TEXT("Material'/Game/CharacterUnit/TeamMarker/MAT_TeamMarker.MAT_TeamMarker'"));
	if (TeamMarkerMaterial.Object)
		TeamMarkerDynamic = (UMaterialInstanceDynamic*) (TeamMarkerMaterial.Object);
		/*Crashes here*/TeamMarkerDynamic->SetVectorParameterValue(FName("TeamMarkerColor"), FLinearColor(100.f, 0.f, 0.f));


void ACharacterUnit::BeginPlay() {
	UGameplayStatics::SpawnDecalAttached(TeamMarkerDynamic, FVector(100.0f, 100.0f, 100.0f), GetRootComponent(),FName(""), GetActorLocation(), FRotator(-90.f, -90.f, 0.f), EAttachLocation::Type(1));

Thank you (a LOT,) that’s the correct approach and I got it working.

I just want to add that according to this post, NULL should be used instead of this. (“this” causes a compilation crash when the function is called in recent engine versions.)

I think you forgot to create your MID first with create like this:

MaterialInstance = UMaterialInstanceDynamic::Create(Material, NULL);

so in your case it should be:

TeamMarkerDynamic = UMaterialInstanceDynamic::Create(TeamMarkerMaterial.Object, NULL);

TeamMarkerDynamic->SetVectorParameterValue(FName("TeamMarkerColor"), FLinearColor(100.f, 0.f, 0.f));

I’m 90% sure you can’t use a cast like you are trying now. Would be great to hear if it’s working with my solution. <3

You are right, I made an edit! <3