AActor::AttachToComponent
#include "GameFramework/Actor.h"
Access: public
Description
Attaches this actor's RootComponent to a specific SceneComponent, with full control over how the existing transform is preserved or reset.
Signature
bool AttachToComponent(USceneComponent* Parent, const FAttachmentTransformRules& AttachmentRules, FName SocketName = NAME_None) Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| Parent | USceneComponent* | The component to attach this actor's RootComponent to. | — |
| AttachmentRules | const FAttachmentTransformRules& | Rules controlling how location, rotation, and scale are handled when attaching. Use FAttachmentTransformRules::KeepWorldTransform to avoid snapping. | — |
| SocketName | FName | Optional socket on the parent component to attach to. | NAME_None |
Return Type
bool Caveats & Gotchas
- • The parent component must be registered before calling — attaching to unregistered components is invalid and returns false.
- • FAttachmentTransformRules::KeepWorldTransform is usually correct for gameplay code (the actor stays in place). KeepRelativeTransform snaps the actor to the component's local origin.
- • The Blueprint version (K2_AttachToComponent) exposes individual rule enums and a bWeldSimulatedBodies flag. Welding can permanently alter the parent's physics body shapes even after subsequent detachment.
Example
Attach a spawned actor to a skeletal mesh socket C++
AWeapon* Weapon = GetWorld()->SpawnActor<AWeapon>(WeaponClass);
if (Weapon)
{
USkeletalMeshComponent* Mesh = GetMesh();
Weapon->AttachToComponent(Mesh, FAttachmentTransformRules::SnapToTargetIncludingScale, TEXT("WeaponSocket"));
} Version History
Introduced in: 4.12
| Version | Status | Notes |
|---|---|---|
| 5.6 | stable | — |
Feedback
Was this helpful?