#unreal

See also Common Actor Component problems

Creating components at runtime

//Create new component using NewObject
auto* NewMesh = NewObject<UStaticMeshComponent>(ParentActor);
 
//RegisterComponent is required
NewMesh->RegisterComponent();
 
//AddInstanceComponent makes the runtime added component visible
//in editor's details panel for the actor, but may be otherwise unnecessary
ParentActor->AddInstanceComponent(NewMesh);

Creating components in OnConstruction

See explanation in https://landelare.github.io/2023/01/07/cpp-speedrun.html#default-subobjects-components