# The component is not attached to the socket via C++ code

**URL:** <https://forums.unrealengine.com/t/the-component-is-not-attached-to-the-socket-via-c-code/610062>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [July 21, 2022, 9:04am UTC](https://forums.unrealengine.com/t/the-component-is-not-attached-to-the-socket-via-c-code/610062 "2022-07-21T09:04:55Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mithndriya](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Mithndriya](https://forums.unrealengine.com/u/Mithndriya)\
**Post date:** [July 21, 2022, 9:04am UTC](https://forums.unrealengine.com/t/the-component-is-not-attached-to-the-socket-via-c-code/610062/1 "2022-07-21T09:04:55Z")

</div>

The component is not attached to the socket via C++ code.

```auto
my header code:
UPROPERTY(EditAnywhere)
	UBoxComponent* RHCollision;

```

```auto
my cpp code:
RHCollision = CreateDefaultSubobject<UBoxComponent>(TEXT("RHCollision"));
RHCollision->SetupAttachment(GetMesh(), TEXT("RightHand"));

```

---

<div class="post-metadata">

**Author:** ![Tuerer](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/tuerer/32/1008648_2.png) [@Tuerer](https://forums.unrealengine.com/u/Tuerer)\
**Post date:** [July 21, 2022, 9:08am UTC](https://forums.unrealengine.com/t/the-component-is-not-attached-to-the-socket-via-c-code/610062/2 "2022-07-21T09:08:57Z")

</div>

In my experience, attaching to sockets never really properly worked in the constructor for some reason.  
Try setting up attachment in either OnConstruction() or BeginPlay() instead.

---

<div class="post-metadata">

**Author:** ![JoSf](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@JoSf](https://forums.unrealengine.com/u/JoSf)\
**Post date:** [July 21, 2022, 9:14am UTC](https://forums.unrealengine.com/t/the-component-is-not-attached-to-the-socket-via-c-code/610062/3 "2022-07-21T09:14:31Z")

</div>

Have you compiled the code with the editor closed?

---

<div class="post-metadata">

**Author:** ![Mithndriya](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Mithndriya](https://forums.unrealengine.com/u/Mithndriya)\
**Post date:** [July 21, 2022, 9:15am UTC](https://forums.unrealengine.com/t/the-component-is-not-attached-to-the-socket-via-c-code/610062/4 "2022-07-21T09:15:23Z")

</div>

Yes

---

<div class="post-metadata">

**Author:** ![DavidCR](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@DavidCR](https://forums.unrealengine.com/u/DavidCR)\
**Post date:** [July 21, 2022, 9:18am UTC](https://forums.unrealengine.com/t/the-component-is-not-attached-to-the-socket-via-c-code/610062/5 "2022-07-21T09:18:59Z")

</div>

I’m not sure about the solution, but I took a look into documentation

![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/4/8/2489b0d8d06a0193730db97aaf2977d45bc4ae68.png)  
Have you tried with a Name variable instead of a TEXT one?

Something like:

```auto
FName RightHand = "RightHand"

RHCollision = CreateDefaultSubobject<UBoxComponent>(TEXT("RHCollision"));
RHCollision->SetupAttachment(GetMesh(), RightHand );

```

---

<div class="post-metadata">

**Author:** ![JoSf](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@JoSf](https://forums.unrealengine.com/u/JoSf)\
**Post date:** [July 21, 2022, 9:21am UTC](https://forums.unrealengine.com/t/the-component-is-not-attached-to-the-socket-via-c-code/610062/6 "2022-07-21T09:21:49Z")

</div>

I’m looking at the syntax I’ve used in my project and it’s FName(“socketname”)

Try using that instead of TEXT(“socketname”)

---

<div class="post-metadata">

**Author:** ![Mithndriya](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Mithndriya](https://forums.unrealengine.com/u/Mithndriya)\
**Post date:** [July 21, 2022, 9:25am UTC](https://forums.unrealengine.com/t/the-component-is-not-attached-to-the-socket-via-c-code/610062/7 "2022-07-21T09:25:44Z")

</div>

does not work(

---

<div class="post-metadata">

**Author:** ![Mithndriya](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Mithndriya](https://forums.unrealengine.com/u/Mithndriya)\
**Post date:** [July 21, 2022, 9:27am UTC](https://forums.unrealengine.com/t/the-component-is-not-attached-to-the-socket-via-c-code/610062/8 "2022-07-21T09:27:16Z")

</div>

does not work

---

<div class="post-metadata">

**Author:** ![JoSf](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@JoSf](https://forums.unrealengine.com/u/JoSf)\
**Post date:** [July 21, 2022, 9:31am UTC](https://forums.unrealengine.com/t/the-component-is-not-attached-to-the-socket-via-c-code/610062/10 "2022-07-21T09:31:00Z")

</div>

Use AttachToComponent instead of SetupAttachment

---

<div class="post-metadata">

**Author:** ![Mithndriya](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Mithndriya](https://forums.unrealengine.com/u/Mithndriya)\
**Post date:** [July 21, 2022, 9:33am UTC](https://forums.unrealengine.com/t/the-component-is-not-attached-to-the-socket-via-c-code/610062/11 "2022-07-21T09:33:50Z")

</div>

does not work(

---

<div class="post-metadata">

**Author:** ![JoSf](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@JoSf](https://forums.unrealengine.com/u/JoSf)\
**Post date:** [July 21, 2022, 9:36am UTC](https://forums.unrealengine.com/t/the-component-is-not-attached-to-the-socket-via-c-code/610062/12 "2022-07-21T09:36:47Z")

</div>

```auto
const FAttachmentTransformRules AttachmentTransformRules(EAttachmentRule::KeepRelative, false);

RHCollision->AttachToComponent(GetMesh(), AttachmentTransformRules, FName("RightHand"));

```

Also verify that the socket exists, usually the syntax is more like “RightHand\_Socket”

---

<div class="post-metadata">

**Author:** ![DavidCR](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@DavidCR](https://forums.unrealengine.com/u/DavidCR)\
**Post date:** [July 21, 2022, 9:37am UTC](https://forums.unrealengine.com/t/the-component-is-not-attached-to-the-socket-via-c-code/610062/13 "2022-07-21T09:37:18Z")

</div>

I got a few questions about your code.

1. How do you know if it’s not attached? Remember that sometimes properties declared in C++ are not visible in Blueprints.  
Try to change your `UPROPERTY(EditAnywhere)` to `UPROPERTY(EditAnywhere, BlueprintReadWrite)` to visualize it on editor.

2. Are you sure “GetMesh()” is initialized by the time you set up attachments to it?  
May be something to confirm.

---

<div class="post-metadata">

**Author:** ![Mithndriya](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Mithndriya](https://forums.unrealengine.com/u/Mithndriya)\
**Post date:** [July 21, 2022, 9:40am UTC](https://forums.unrealengine.com/t/the-component-is-not-attached-to-the-socket-via-c-code/610062/14 "2022-07-21T09:40:32Z")

</div>

the component is attached to the mesh, but not attached to the socket

---

<div class="post-metadata">

**Author:** ![JoSf](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@JoSf](https://forums.unrealengine.com/u/JoSf)\
**Post date:** [July 21, 2022, 9:45am UTC](https://forums.unrealengine.com/t/the-component-is-not-attached-to-the-socket-via-c-code/610062/15 "2022-07-21T09:45:32Z")

</div>

The socket name is a suspect, the autogenerated ones are like “bonename\_##socket”

Check the socket name or if it even exists on the skeleton of your skeletal mesh asset

---

<div class="post-metadata">

**Author:** ![Mithndriya](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Mithndriya](https://forums.unrealengine.com/u/Mithndriya)\
**Post date:** [July 21, 2022, 9:47am UTC](https://forums.unrealengine.com/t/the-component-is-not-attached-to-the-socket-via-c-code/610062/16 "2022-07-21T09:47:35Z")

</div>

Such a socket exists. I created it myself

---

<div class="post-metadata">

**Author:** ![JoSf](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@JoSf](https://forums.unrealengine.com/u/JoSf)\
**Post date:** [July 21, 2022, 9:53am UTC](https://forums.unrealengine.com/t/the-component-is-not-attached-to-the-socket-via-c-code/610062/17 "2022-07-21T09:53:12Z")

</div>

Ah, yeah.

I used SetRelativeLocation to adjust the component position before using AttachToComponent.  
For me that worked better than snapping.

You can either use SnapToTarget AttachmentTransformRule if it works for you as is, or use SetRelativeLocation to first adjust the position of the component to attach on spawn and then use the keep relative attachmenttransformrule.

---

<div class="post-metadata">

**Author:** ![Mithndriya](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Mithndriya](https://forums.unrealengine.com/u/Mithndriya)\
**Post date:** [July 28, 2022, 7:48pm UTC](https://forums.unrealengine.com/t/the-component-is-not-attached-to-the-socket-via-c-code/610062/18 "2022-07-28T19:48:57Z")

</div>

I found a solution. In fact, the object is attached to the socket, it’s just not displayed)

---

<div class="post-metadata">

**Author:** ![TheGreatDanish](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/thegreatdanish/32/770968_2.png) [@TheGreatDanish](https://forums.unrealengine.com/u/TheGreatDanish)\
**Post date:** [July 22, 2024, 11:30am UTC](https://forums.unrealengine.com/t/the-component-is-not-attached-to-the-socket-via-c-code/610062/19 "2024-07-22T11:30:17Z")

</div>

It works. Set it up as this, and it is exactly where it should be and doing exactly what I’d expect. Thanks for the tip.

```auto
WeaponCollider = CreateDefaultSubobject<UBoxComponent>(TEXT("WeaponCollider"));
WeaponCollider->SetupAttachment(GetMesh(), FName("Collision_Sword"));

```
