How to stop component going to world root

I’m new to c++ and trying to learn more about it so if I come off as stupid I’m sorry
I’ve made an actor with 2 components but whenever there is a component attached to the root component when I place the object down the attached component ends up moving to the world root I’ve been experiencing this since I’ve started with c++ and I was wondering if there is any fix?

Can you post the code please?

c++

#include "Door.h"


// Sets default values
ADoor::ADoor()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	StaticMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMeshComponent"));
	RootComponent = StaticMeshComponent;

	StaticMeshComponentPart2 = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMeshComponentPart2"));

	

}

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

// Called every frame
void ADoor::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

header

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Components/ArrowComponent.h"
#include "Components/StaticMeshComponent.h"
#include "Door.generated.h"

UCLASS()
class HN_MEMORIES_API ADoor : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	ADoor();

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Components")
		class UStaticMeshComponent* StaticMeshComponent;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Components")
	class UStaticMeshComponent* StaticMeshComponentPart2;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Components")
		class UArrowComponent* Arrow;

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	
	
};

Try adding this line to your .cpp:

StaticMeshComponentPart2->SetupAttachment(RootComponent);

Didn’t work

Edit: I just had to turn it into a blueprint again Thanks! I knew it was a thing but I thought it would’ve been something that Ue4 would’ve set up on its own once it knows what the root component is

I just created the same file in a blank project it works as expected once added this line.

Did you try dragging the C++ file directly from ContentBrowser to the scene? (Don’t create BP yet)