# Component in wrong location and not attached

**URL:** <https://forums.unrealengine.com/t/component-in-wrong-location-and-not-attached/2247120>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine, question\
**Created:** [December 28, 2024, 11:13am UTC](https://forums.unrealengine.com/t/component-in-wrong-location-and-not-attached/2247120 "2024-12-28T11:13:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anonymous\_589346a4dc419de069438765086d9ea0](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Anonymous\_589346a4dc419de069438765086d9ea0](https://forums.unrealengine.com/u/Anonymous_589346a4dc419de069438765086d9ea0)\
**Post date:** [December 28, 2024, 11:13am UTC](https://forums.unrealengine.com/t/component-in-wrong-location-and-not-attached/2247120/1 "2024-12-28T11:13:36Z")

</div>

Hello!

I have tried to make a custom component which has funtionalities as flashlight and is attached to a character.

I made the Flashlight Component and Character in C++ class first, and create them as blueprints based on the c++.

The custom component is attached fine in the blueprint editor, but if I start game to test, the component in wrong location, world location 0,0,0, not attached to the character. Also the components are separate…

Could anybody help about this problem, if I code wrong or should add something more?

1. component.h

```auto
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class MYPROJECT_API UFlashlightComponent : public USceneComponent
{
	GENERATED_BODY()

...
public:
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	TObjectPtr<USpringArmComponent> LightSpringArm;

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	TObjectPtr<USpotLightComponent> SpotLight;

```

1. component.cpp

```auto
UFlashlightComponent::UFlashlightComponent()
{
	PrimaryComponentTick.bCanEverTick = true;

	bWantsInitializeComponent = true;

	LightSpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("LightSpringArmComponent"));
	LightSpringArm->TargetArmLength = 0.f;
	LightSpringArm->bUsePawnControlRotation = true;
	LightSpringArm->bEnableCameraRotationLag = true;
	LightSpringArm->CameraRotationLagSpeed = 12.f;
	LightSpringArm->SetupAttachment(this);

	SpotLight = CreateDefaultSubobject<USpotLightComponent>(TEXT("SpotLightComponent"));
	SpotLight->AttenuationRadius = 800.f;
	SpotLight->InnerConeAngle = 23.5f;
	SpotLight->OuterConeAngle = 31.f;
	SpotLight->SetVisibility(false);
	SpotLight->SetLightColor(FLinearColor(255, 250, 227));
	SpotLight->SetupAttachment(LightSpringArm);
}

```

Here I also tried AttachToComponent with RelativeLocation rule instead of SetupAttachment, but it had also same problem.

1. character.cpp

```auto
APlayerCharacter::APlayerCharacter()
{
	PrimaryActorTick.bCanEverTick = false;

...
	FlashlightComponent = CreateDefaultSubobject<UFlashlightComponent>(TEXT("FlashlightComponent"));
       FlashlightComponent->SetupAttachment(GetCapsuleComponent());
...
}

```

1. In Blueprint Editor  

2. In Game

---

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [December 28, 2024, 3:52pm UTC](https://forums.unrealengine.com/t/component-in-wrong-location-and-not-attached/2247120/2 "2024-12-28T15:52:06Z")

</div>

Nested components don’t work well when instantiated outside of the main actor. Either spawn all of the components on the main actor or have the flashlight be it’s own actor and attach it to the character.

Unreal’s architecture is a bit stupid in that way. It’s not like game objects in Unity. Each component goes through it’s own registry and initialization process and nested components for some reason don’t work well with this process.

If someone has had better luck with nested components then I’m all ears.

I tried a version where you all the attach code later down the line via a setup command

```auto
void UFlashlightComponent::SetupFlashlight(USceneComponent* parent)
{
	SpotLight->SetupAttachment(LightSpringArm);
	LightSpringArm->SetupAttachment(parent);
}

```

Then later called inside of the character CDO

```auto
FlashlightComponent = CreateDefaultSubobject<UFlashlightComponent>(TEXT("FlashlightComponent"));

FlashlightComponent->SetupFlashlight(GetCapsuleComponent());

```

[FlashlightComponent.cpp](https://forums.unrealengine.com/uploads/short-url/ajyk61EsW56hj4UYKxRImJSjPcT.cpp) (1.7 KB)  
[FlashlightComponent.h](https://forums.unrealengine.com/uploads/short-url/gHIHDojVktnF9WHR0vXS5PdYvhw.h) (1.0 KB)  
[FlashLightTestCharacter.cpp](https://forums.unrealengine.com/uploads/short-url/2CRXJjJ2pyfCue2u6hKiFOLsmFW.cpp) (5.6 KB)  
[FlashLightTestCharacter.h](https://forums.unrealengine.com/uploads/short-url/peRbH1spucOB3sTYf8oGL63pia9.h) (2.3 KB)

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/a/d/7/ad75e66fbff9c9f9de8902165fcc5698e45619a4.jpeg)

With visibility turned on

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/0/c/c0ce6c16ac8225afd12b07c28fb39fc38efd63bd.jpeg)

---

<div class="post-metadata">

**Author:** ![Anonymous\_589346a4dc419de069438765086d9ea0](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Anonymous\_589346a4dc419de069438765086d9ea0](https://forums.unrealengine.com/u/Anonymous_589346a4dc419de069438765086d9ea0)\
**Post date:** [December 28, 2024, 10:24pm UTC](https://forums.unrealengine.com/t/component-in-wrong-location-and-not-attached/2247120/3 "2024-12-28T22:24:09Z")

</div>

You saved me, thank you so much!!!

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [January 27, 2025, 10:25pm UTC](https://forums.unrealengine.com/t/component-in-wrong-location-and-not-attached/2247120/4 "2025-01-27T22:25:03Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
