How do i spawn a component?

I need to Spawn a component and attach it to another when i press a action mapping (C).
Every time i try this method SpawnChildren the UE4 breaks.

Here the .h (simplified)

UCLASS()
class HOWTO_COMPONENTS_API ACollidingPawn : public APawn
{
	GENERATED_BODY()

private:
	UPROPERTY()
	UStaticMeshComponent* OrbitSphereVisual;

	UFUNCTION()
	void SpawnChildren();

	UPROPERTY()
	float SpawnTime;

};

The .cpp (simplified)

ACollidingPawn::ACollidingPawn()
{
	PrimaryActorTick.bCanEverTick = true;

	USphereComponent* SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("RootComponent"));
	RootComponent = SphereComponent;
	SphereComponent->InitSphereRadius(40.0f);
	SphereComponent->SetCollisionProfileName(TEXT("Pawn"));

	UStaticMeshComponent* SphereVisual = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("VisualRepresentation"));
	SphereVisual->SetupAttachment(RootComponent);
	static ConstructorHelpers::FObjectFinder<UStaticMesh> SphereVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere"));
	if (SphereVisualAsset.Succeeded())
	{
		SphereVisual->SetStaticMesh(SphereVisualAsset.Object);
		SphereVisual->SetRelativeLocation(FVector(0.0f, 0.0f, -40.0f));
		SphereVisual->SetRelativeScale3D(FVector(0.8f));
	}

	OrbitSphereVisual = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("OrbitSphereVisual"));
	OrbitSphereVisual->SetupAttachment(SphereComponent);
	static ConstructorHelpers::FObjectFinder<UStaticMesh> OrbitSphereVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere"));
	if (OrbitSphereVisualAsset.Succeeded())
	{
		OrbitSphereVisual->SetStaticMesh(OrbitSphereVisualAsset.Object);
		OrbitSphereVisual->SetRelativeLocation(FVector(0.0f, 100.0f, 0.0f)); 
		OrbitSphereVisual->SetRelativeScale3D(FVector(0.2f));
	}
}

// Called when the game starts or when spawned
void ACollidingPawn::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called to bind functionality to input
void ACollidingPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);
	PlayerInputComponent->BindAction("Spawn", IE_Pressed, this, &ACollidingPawn::SpawnChildren);

}


void ACollidingPawn::SpawnChildren()
{
	UStaticMeshComponent* newMesh = NewObject<UStaticMeshComponent>(this->GetOwner());
	newMesh->SetupAttachment(OrbitSphereVisual);

	static ConstructorHelpers::FObjectFinder<UStaticMesh> newMeshAsset(TEXT("/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere"));
	if (newMeshAsset.Succeeded())
	{
		newMesh->SetStaticMesh(newMeshAsset.Object);
		newMesh->SetRelativeLocation(FVector(0.0f, 50.0f, 0.0f));
	}
}

Anything from ConstructorHelpers as may suggests can not be used outside constructor. You need UStaticMesh* and there many way to get it.

Most easiest, if this class gonna have a blueprint, make UPROPERTY(EditDefaultsOnly) UStaticMesh* ChildMesh (so it editable only in defaults) and set it in blueprint defaults.

If ConstructorHelpers::FObjectFinder can be called only in constructor… then do it in constructor, make a UPROPERTY and keep the return of that function for later to use, you dont need to do this just before component creation

SomeStaticMeshPropertyYouMade = ConstructorHelpers::FObjectFinder<UStaticMesh>(TEXT("/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere"));

Use soft object path:

Or talk to AssetRegistry directly which is asset database, this solution let you get entire sets of assets at once and also let you do some more dynamic UI (UI dependent on asset list):

https://docs.unrealengine.com/en-US/API/Runtime/AssetRegistry/IAssetRegistry/index.html

Generally or else you really need to (for example you not plan making blueprints), you should not hard code asset in C++ as path changing may mess up things. Also make sure your solution works after packaging, if asset is nto referenced anywhere it may not be packaged

Just under the NewObject function, try adding NewMesh->RegisterComponent().