[Question]Edit Material Parameters C++

I have an object that I would like to make blink on and off, I was able to get it to work in Blueprint just fine. But when trying to do the same with C++ it builds with no errors but when tested in the editor it crashes with no error message.
I have gone through my code and found out I am crashing anytime I try to get the material of the static mesh itself. My Code is very simple right now, but I can’t seem to figure out what I am doing wrong.

.h

UMaterialInstanceDynamic* MILight;

Inside of my function I am running this code.

UMaterialInstanceDynamic* MILight = UMaterialInstanceDynamic::Create(Lamp->GetMaterial(0),this); // This is where I appear to be crashing at.

MILight->SetVectorParameterValue(FName(TEXT("LightColor")),FLinearColor(0.0,0.0,0.0,1.0);

Lamp->SetMaterial(0, MILight);

Any help would be greatly appreciated!

Dear Tyler,

Please post your code where you are setting Lamp pointer properly.

Most likely your Lamp is not valid reference

to test this, modify your code as follows, probably you will not get a crash but still nothing will happen, because you are not setting the Lamp reference properly.

if(!Lamp) return;
//~~~~~~~~~~~~

UMaterialInstanceDynamic* MILight = UMaterialInstanceDynamic::Create(Lamp->GetMaterial(0),this); // This is where I appear to be crashing at.
 
MILight->SetVectorParameterValue(FName(TEXT("LightColor")),FLinearColor(0.0,0.0,0.0,1.0);
 
Lamp->SetMaterial(0, MILight);
1 Like

Okay, the pointer is where I went wrong. It is all working now, was a small mistake on my end. Thank you!