# Attaching an Actor on an Skeletal Mesh Socket

**URL:** <https://forums.unrealengine.com/t/attaching-an-actor-on-an-skeletal-mesh-socket/107082>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [February 26, 2018, 3:12pm UTC](https://forums.unrealengine.com/t/attaching-an-actor-on-an-skeletal-mesh-socket/107082 "2018-02-26T15:12:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_38e80d6e](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_38e80d6e](https://forums.unrealengine.com/u/anonymous_user_38e80d6e)\
**Post date:** [February 26, 2018, 3:12pm UTC](https://forums.unrealengine.com/t/attaching-an-actor-on-an-skeletal-mesh-socket/107082/1 "2018-02-26T15:12:00Z")

</div>

Hi everyone !  
As a beginner on Unreal Engine, I have many issues with attaching an actor (which is basically a weapon) to the hand of my Character !  
Here is where I tried to attach the weapon to the socket

```auto

AHumanoide::AHumanoide()
{
    FName fnWeaponSocket = TEXT("hand_lSocket");
    UWorld* World = GetWorld();
    if (World)
    {
        arc=GetWorld()->SpawnActor<AArmeADistance>(AArmeADistance::StaticClass());
        arc->AttachRootComponentTo(GetMesh(), fnWeaponSocket, EAttachLocation::SnapToTarget, true);
    }
}

```

And here is the constructor of my weapon

```auto

AArmeADistance::AArmeADistance()
{
    RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
    //définition du mesh
    StaticMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
    static ConstructorHelpers::FObjectFinder<UStaticMesh> mesh(TEXT("StaticMesh'/Game/Projet_Dragonnier/Equipement/Armes/ADistance/Arc/Arc.Arc'"));
    StaticMeshComponent->SetStaticMesh(mesh.Object);
    StaticMeshComponent->SetSimulatePhysics(false);
    StaticMeshComponent->SetCollisionEnabled(ECollisionEnabled::NoCollision);
    StaticMeshComponent->SetupAttachment(RootComponent);
}

```

I’ve tried many things but the weapon is still spawning at the origin and never in the hand off my character …  
Could someone help me ? ☹  
Thanks you a lot !

---

<div class="post-metadata">

**Author:** ![Phantas](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Phantas](https://forums.unrealengine.com/u/Phantas)\
**Post date:** [February 26, 2018, 4:22pm UTC](https://forums.unrealengine.com/t/attaching-an-actor-on-an-skeletal-mesh-socket/107082/2 "2018-02-26T16:22:36Z")

</div>

If you are using character, shouldn’t you use SkeletalMesh, instead of StaticMesh?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_38e80d6e](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_38e80d6e](https://forums.unrealengine.com/u/anonymous_user_38e80d6e)\
**Post date:** [February 26, 2018, 4:27pm UTC](https://forums.unrealengine.com/t/attaching-an-actor-on-an-skeletal-mesh-socket/107082/3 "2018-02-26T16:27:30Z")

</div>

AArmeADistance is the class for the Weapon, which is an actor, so I think using a StaticMesh is correct no ?  
In the class AHumanoide, which extends ACharacter, the Mesh is indeed a SkeletalMesh (that I get with GetMesh()) 😉

---

<div class="post-metadata">

**Author:** ![Phantas](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Phantas](https://forums.unrealengine.com/u/Phantas)\
**Post date:** [February 26, 2018, 4:30pm UTC](https://forums.unrealengine.com/t/attaching-an-actor-on-an-skeletal-mesh-socket/107082/4 "2018-02-26T16:30:46Z")

</div>

yep, sorry my bad.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_38e80d6e](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_38e80d6e](https://forums.unrealengine.com/u/anonymous_user_38e80d6e)\
**Post date:** [February 27, 2018, 11:17am UTC](https://forums.unrealengine.com/t/attaching-an-actor-on-an-skeletal-mesh-socket/107082/5 "2018-02-27T11:17:14Z")

</div>

Found the problem !  
The best way to do what I wanted to do is to spawn and attach the actor in the BeginPlay() method :

```auto

void AHumanoide::BeginPlay()
{
    Super::BeginPlay();
    FName fnWeaponSocket = TEXT("hand_lSocket");
    FTransform socketTransform = GetMesh()->GetSocketTransform(fnWeaponSocket, ERelativeTransformSpace::RTS_World);
    FActorSpawnParameters parametres;
    parametres.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::Undefined;
    ArmeADistance = GetWorld()->SpawnActor<AArmeADistance>(AArmeADistance::StaticClass(), socketTransform);
    if (ArmeADistance)
    {
        ArmeADistance->AttachToComponent(GetMesh(), FAttachmentTransformRules(EAttachmentRule::SnapToTarget, true), fnWeaponSocket);
    }

}

```

It is now perfectly working, hope it will help another beginner like me in the future ! 😉
