Component does not move with its actor

I have a problem and I found no reason, why it happens and how I solve it:

I have a paper character, that adds some components to it:

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

	SwordVisual = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("SwordVisual"));
	static ConstructorHelpers::FObjectFinder<UStaticMesh> SwordVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere"));
	if (SwordVisualAsset.Succeeded())
	{
		SwordVisual->SetStaticMesh(SwordVisualAsset.Object);
		SwordVisual->SetWorldScale3D(FVector(0.8f));
	}
    SwordVisual->AttachTo(RootComponent);

	SwordSphere = CreateDefaultSubobject<USphereComponent>(TEXT("SwordSphere"));
	SwordSphere->AttachTo(SwordVisual);
	SwordSphere->InitSphereRadius(40.0f);
	SwordSphere->SetCollisionProfileName(TEXT("Pawn"));
}

SwordVisual and SwordSphere are properties of AEntity. The class compiles without any problems, but when I try to run it, it looks like this:
64029-
The Sphere doesn’t attach to the character and just stays at the center of the level!
What did I wrong?

The issue must be within the root component.
Try checking if it is NULL.

Try setting mobility:

If component is static it won’t follow the root, also check as StreakyCat if root component and component structure is set right. Best way to debug components in C++ is to templerly create blueprint based of your C++ class, the blueprint editor will show the component structure that your constructor code creates.

Looking on you visuals, you might consider checking out Paper2D, maybe it will be batter for your game:

I’m sorry, but it doesn’t work. The sphere stays at the center of the level.

I had the exact same problem. Seeting a proper AttachType fixed it for me, since the default is “EAttachLocation::KeepRelativeOffset” which causes the component to stay where it is spawned i guess.

Following adjustment led to correct behaviour (at least for me):

SphereTracer->AttachTo(RootComponent, TEXT("NAME_None"), EAttachLocation::SnapToTargetIncludingScale, false);
1 Like

This saved me, thanks!