Setting material instance parameter not working.

So I’m testing this on a simple cube object. The parent material has a single scalar parameter called “RedEmissive” and it makes the material glow red. I test changing this parameter in the Material editor and it works fine.

I make an instance of this parent material. I apply this instance material to the mesh in the editor. I use this mesh as the Mesh component on my object blueprint. In the C++ class for this object I do the following:

MyClass.h

UPROPERTY()
UMaterialInstanceDynamic* MI_ObjectMat;


MyClass.cpp

// in the constructor
MI_ObjectMat = Mesh->CreateAndSetMaterialInstanceDynamic(0);

// in some other function that gets called
PRINT_SCREEN("SET RED");
MI_ObjectMat->SetScalarParameterValue(TEXT("RedEmissive"), 0.5f);

Now, I see SET RED getting printed to screen, but the color doesn’t change.

What am I doing wrong here… this seems very straight forward.

Sigh. It was a simple mistake.

I had to move:

MI_ObjectMat = Mesh->CreateAndSetMaterialInstanceDynamic(0);

from the constructor to the PostInitializeComponents function.

Why the editor wasn’t crashing with a null pointer to the MI_ObjectMat like it should have is beyond me.

I’ll leave this here for others with the same mistake.

Strange. I was able to set it in the constructor on an Actor, but on my Pawn I had to use this method.