Create MaterialInstance inside constructor

I’m new on Unreal Engine development and I’m trying this code in a class constructor:

static ConstructorHelpers::FObjectFinder<UMaterial> MatFinder(TEXT("Material'/Game/Geometry/Materials/Star.Star'"));
if (MatFinder.Succeeded())
{
	StarMaterial = MatFinder.Object;

	UMaterialInstance* matInstance = UMaterialInstanceDynamic::Create(StarMaterial, this);

But in UMaterialInstance* matInstance = UMaterialInstanceDynamic::Create(StarMaterial, this); I get this error:

NewObject with empty name can’t be
used to create default subobjects
(inside of UObject derived class
constructor) as it produces
inconsistent object names. Use
ObjectInitializer.CreateDefaultSuobject<>
instead.

I get this error because I’m using Create inside a constructor. Is there any other way to create a MaterialInstance inside a constructor? or maybe, where can I create this MaterialInstance.

Hello,

There is a problem creating you dynamic material instance in the constructor.
What I did is the following.

In the constructor:

AAAAA::AAAAA(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)
{
	static ConstructorHelpers::FObjectFinder<UMaterialInstance> IconEnergyFillMatOb(TEXT("/Game/HUD/Materials/M_IconEnergyLevel_Inst"));

if (IconEnergyFillMatOb.Succeeded())
	IconEnergyFillMatInst = IconEnergyFillMatOb.Object;

}

In the class AAAAA
UMaterialInstance* IconEnergyFillMatInst;
UMaterialInstanceDynamic* IconEnergyFillMatInstDyn;

In PostInitialize :

void AAAAA::PostInitializeComponents()
{
	Super::PostInitializeComponents();

	if (IconEnergyFillMatInst)
		IconEnergyFillMatInstDyn = UMaterialInstanceDynamic::Create(IconEnergyFillMatInst, this);
}

The FObjectFinder uses a UMaterialInstance
And UMaterialInstanceDynamic::Create returns a UMaterialInstanceDynamic

I hope this will help you.
D.