This tutorial is misleading. We cannot attach components to uninitialized AActor::RootComponent

Misleading QuickStart Tutorial

VisualMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
VisualMesh->SetupAttachment(RootComponent);

static ConstructorHelpers::FObjectFinder<UStaticMesh> CubeVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Cube.Shape_Cube"));

if (CubeVisualAsset.Succeeded())
{
    VisualMesh->SetStaticMesh(CubeVisualAsset.Object);
    VisualMesh->SetRelativeLocation(FVector(0.0f, 0.0f, 0.0f));
}

How can we attach components to RootComponent that is still nullptr?

Hello pstricks.fans,

Thank you for bringing this to our attention. We have logged a ticket with our Documentation Team to get this page updated, in the meantime, as a workaround you can declare the following:

if (USceneComponent* ExistingRootComponent = GetRootComponent())
{
     VisualMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
     VisualMesh->SetupAttachment(RootComponent);

     static ConstructorHelpers::FObjectFinder<UStaticMesh> 
CubeVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Cube.Shape_Cube"));

    if (CubeVisualAsset.Succeeded())
    {
      VisualMesh->SetStaticMesh(CubeVisualAsset.Object);
      VisualMesh->SetRelativeLocation(FVector(0.0f, 0.0f, 0.0f));
    }

}

Thanks!

As by default AActor::GetRootComponent() returns NULL, the body of the if construct will never be executed (the condition is always false). As a result the actor will not work as the tutorial expects.

The proper approach is instantiating a component and set it as the root component.
The if construct is not needed at all.