USceneComponent::AttachToComponent
#include "Components/SceneComponent.h"
Access: public
Specifiers: virtualUFUNCTIONBlueprintCallable
Description
Attaches this component to another scene component at runtime, optionally at a named socket. Returns true if the attachment succeeded.
Signature
virtual bool AttachToComponent(USceneComponent* InParent, const FAttachmentTransformRules& AttachmentRules, FName InSocketName = NAME_None) Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| InParent | USceneComponent* | The component to attach to. | — |
| AttachmentRules | const FAttachmentTransformRules& | Controls how location, rotation, and scale are handled on attach (KeepRelative, KeepWorld, SnapToTarget). | — |
| InSocketName | FName | Optional socket name on the parent to attach to. | NAME_None |
Return Type
bool Caveats & Gotchas
- • In constructors, prefer SetupAttachment() — AttachToComponent() is for runtime use after registration.
- • FAttachmentTransformRules::KeepWorldTransform preserves the world position; KeepRelativeTransform preserves the relative offset. Choosing wrong here is the most common source of 'snap on attach' bugs.
- • Returns false if attachment is rejected (e.g. circular attachment, invalid parent). Always check the return value in editor builds.
Example
Attach a weapon mesh to a hand socket at runtime C++
WeaponMesh->AttachToComponent(
CharacterMesh,
FAttachmentTransformRules::SnapToTargetNotIncludingScale,
TEXT("hand_r")
); Tags
Version History
Introduced in: 4.0
| Version | Status | Notes |
|---|---|---|
| 5.6 | stable | — |
Feedback
Was this helpful?