I’m posting few lines of code just to keep the topic up to date since ConstructObject and AttachTo are deprecated:
void AActorWithInstancedComponent::OnConstruction(const FTransform& Transform)
{
Super::OnConstruction(Transform);
class UTestComponent* NewComponent = NewObject<UTestComponent>(this,UTestComponent::StaticClass(), TEXT("NewComponent"));
NewComponent ->RegisterComponent();
NewComponent ->AttachToComponent(GetRootComponent(), FAttachmentTransformRules::KeepRelativeTransform);
}
AttachToComponent() only works for USceneComponent and its children and not for UActorComponent