What is the correct way to create and add components at runtime ?

Somehow I think the following lines are more correct for constructing components:

USphereComponent* SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT(“RootComponent”));
RootComponent = SphereComponent;

or

UStaticMeshComponent* SphereVisual = CreateDefaultSubobject<UStaticMeshComponent>(TEXT(“VisualRepresentation”));
SphereVisual->AttachTo(RootComponent);

They’re according to this official tutorial.

1 Like