AActor::AttachToActor
#include "GameFramework/Actor.h"
Access: public
Description
Attaches this actor's RootComponent to the RootComponent of another actor. The attached actor will follow the parent's transform.
Signature
bool AttachToActor(AActor* ParentActor, const FAttachmentTransformRules& AttachmentRules, FName SocketName = NAME_None) Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| ParentActor | AActor* | The actor to attach this actor's RootComponent to. | — |
| AttachmentRules | const FAttachmentTransformRules& | Rules controlling how location, rotation, and scale are handled at the point of attachment. | — |
| SocketName | FName | Optional socket on the parent actor's RootComponent to attach to. | NAME_None |
Return Type
bool Caveats & Gotchas
- • This attaches root-to-root. To attach to a specific component or socket on the parent, use AttachToComponent() instead.
- • Destroying the parent actor will also destroy attached child actors. To keep the child alive after the parent is destroyed, detach before destroying the parent.
Example
Attach a dropped item actor to a character's hand socket C++
// In AMyCharacter — note: use AttachToComponent if you need a specific socket
// on a component rather than the character root.
APickup* Item = GetWorld()->SpawnActor<APickup>(PickupClass);
if (Item)
{
Item->AttachToActor(this, FAttachmentTransformRules::SnapToTargetNotIncludingScale);
} Version History
Introduced in: 4.12
| Version | Status | Notes |
|---|---|---|
| 5.6 | stable | — |
Feedback
Was this helpful?