Mesh material type?

Hi Everyone,

I’m learning how to use dynamic material instances on procedural meshes and I’ve got myself a bit stuck.

Basically I have a line in my actors OnConstruction method doing this:


edenTestMaterialInstanceDynamic = UMaterialInstanceDynamic::Create(testPlane->GetMaterial(0), this);

( and I setup the var in the actors header with UMaterialInstanceDynamic* edenTestMaterialInstanceDynamic; )

It works first time, but after that it fails with this error message:
LogMaterial:Warning: MaterialInstanceDynamic /Game/Core/Maps/Test_planet_level.Test_planet_level:PersistentLevel.edenPlanet_11.MaterialInstanceDynamic_38 is not a valid parent for MaterialInstanceDynamic /Game/Core/Maps/Test_planet_level.Test_planet_level:PersistentLevel.edenPlanet_11.MaterialInstanceDynamic_39. Only Materials and MaterialInstanceConstants are valid parents for a material instance.

Makes sense, it can “upgrade” the original material to a material instance, but it can’t then upgrade the new material instance to another material instance.

What I haven’t been able to figure out is how to check if the material in testPlane->GetMaterial(0) is a material or a material instance :frowning: Any tips?

I can’t help you directly, but try typing



testPlane->GetMaterial(0).
testPlane->GetMaterial(0)->


And see what comes up on intellisense. There may be some values like ‘IsInstance’ or something that can help you differentiate. :slight_smile:
Or you can possibly check a value that’s only present on instances, etc.

Maybe even store the first material to a static variable.
Then if the static variable exists, create all further instances from that.

Ah, thank you Juice-Tin, that was the trick. Turns out what I wanted was “IsA”:


	if (testPlane->GetMaterial(0)->IsA(UMaterialInstanceDynamic::StaticClass())) {
		UE_LOG(LogTemp, Warning, TEXT("test planes material is already a UMaterialInstanceDynamic"));
	} else {
		edenTestMaterialInstanceDynamic = UMaterialInstanceDynamic::Create(testPlane->GetMaterial(0), this);
	}