How do I change a StaticMesh's in UStaticMeshComponent without affecting the original mesh itself?

I’m creating a dynamic material for the mesh I’m using in the UStaticMeshComponent but as you can see the original mesh is being edited. Following the code

UStaticMesh* LocalPlaneMesh = ConstructorHelpers::FObjectFinder<UStaticMesh>(TEXT("/Engine/BasicShapes/Plane")).Object;
this->PlaneMaterial = UMaterialInstanceDynamic::Create(
	ConstructorHelpers::FObjectFinder<UMaterial>(TEXT("/Path/To/Material")).Object,
	this->GetWorld()
);
LocalPlaneMesh->SetMaterial(0, this->PlaneMaterial);
this->PlaneComponent->SetStaticMesh(LocalPlaneMesh);

But the following happens

Is there a way to only change the material locally?

afaik, when you assign the static mesh to your local component, mesh itself is still the engine mesh.

I think you should use component, rather than setting to the mesh itself.

MeshComponent->SetMaterial(0,SomeMaterial);

I have tried that and the material nothing happens. It is like there was no change at all.

I’ve found the issue. I was creating the dynamic material and trying to set the component’s material in the constructor. After moving it to BeginPlay I can set it to the component and it works :man_shrugging:

Yeah think in the begin play when its spawned it becomes an instance so won’t cause an issue. Still should be able to do in constructor too. Think this InstancedStaticMesh component can be usefull on those (I didn’t use)

It looks like that in the constructor the components are not initialized yet so setting the material won’t work. Another solution is using PostInitializeComponents.

https://forums.unrealengine.com/t/ustaticmeshcomponent-setmaterial-working-in-beginplay-but-not-in-constructor/82128

1 Like