# AttachToComponent called twice before actually snap to socket

**URL:** <https://forums.unrealengine.com/t/attachtocomponent-called-twice-before-actually-snap-to-socket/499959>\
**Category:** C++\
**Tags:** actorcomponent, unreal-engine, UE5-0, question, UE4-27\
**Created:** [February 19, 2022, 4:47pm UTC](https://forums.unrealengine.com/t/attachtocomponent-called-twice-before-actually-snap-to-socket/499959 "2022-02-19T16:47:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![luchetti88](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@luchetti88](https://forums.unrealengine.com/u/luchetti88)\
**Post date:** [February 19, 2022, 4:47pm UTC](https://forums.unrealengine.com/t/attachtocomponent-called-twice-before-actually-snap-to-socket/499959/1 "2022-02-19T16:47:47Z")

</div>

Hi all,

In my custom ACharacter class I have a simple method as the one below:

```auto
void AMyCharacter::ActivateInteraction()
{
	if(CurrentInteractableItem)
	{
		if(AMyEquip* Equip = Cast<AMyEquip>(CurrentInteractableItem.GetObject()))
		{
			Equip->AttachToComponent(GetMesh(), FAttachmentTransformRules::SnapToTargetIncludingScale, TEXT("ToolSocket"));
		}
	}
}

```

I cannot figure out what is wrong but it essentially needs to be executed twice before attaching the Equip to the skeletal mesh.

And with “need to be” I intend that in debug mode i see that on first call this line of code get called and executed

```auto
Equip->AttachToComponent(GetMesh(), FAttachmentTransformRules::SnapToTargetIncludingScale, TEXT("ToolSocket"));

```

but it does nothing.  
If I call it again the Equip snaps to the socket.

I’m sure is something trivial I’m missing 🙂 any help will be really appreciated.

Othere relevant (or maybe not) infos are:

- CurrentInteractableItem is a TScriptInterface;
- GetMesh() came from USkeletalMeshComponent::GetMesh();
- I also tried FName(TEXT(“ToolSocket”)) instead of TEXT(“ToolSocket”) as SocketName without success.

Thanks in advance

Luca

---

<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:** [February 19, 2022, 7:07pm UTC](https://forums.unrealengine.com/t/attachtocomponent-called-twice-before-actually-snap-to-socket/499959/2 "2022-02-19T19:07:03Z")

</div>

Sockets names are usually FName, not FText; if this code works, maybe FText can be implicitly converted to FName. You can try FName(“ToolSocket”), but I don’t think that’s the issue, because if socket is not found, it will normally get attached to the component origin instead.

---

<div class="post-metadata">

**Author:** ![luchetti88](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@luchetti88](https://forums.unrealengine.com/u/luchetti88)\
**Post date:** [February 19, 2022, 11:22pm UTC](https://forums.unrealengine.com/t/attachtocomponent-called-twice-before-actually-snap-to-socket/499959/3 "2022-02-19T23:22:58Z")

</div>

Yepp that’s not the problem. To double check I tried your suggestion and nothing changed. The behavioural strangeness is that the equip attach himself to the skeletal socket (so the socket is always recognized) but only on second attempt. The first attempt always fails

---

<div class="post-metadata">

**Author:** ![luchetti88](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@luchetti88](https://forums.unrealengine.com/u/luchetti88)\
**Post date:** [February 19, 2022, 11:38pm UTC](https://forums.unrealengine.com/t/attachtocomponent-called-twice-before-actually-snap-to-socket/499959/4 "2022-02-19T23:38:09Z")

</div>

I actually did solve the problem changing the AttachmentTransformRules from this one:

```auto
Equip->AttachToComponent(GetMesh(), FAttachmentTransformRules::SnapToTargetIncludingScale, TEXT("ToolSocket"));

```

with this one

```auto
Equip->AttachToComponent(GetMesh(), FAttachmentTransformRules(EAttachmentRule::SnapToTarget, true), TEXT("ToolSocket"));

```

I think the `true` boolean did the job (it refers to `bInWeldSimulatedBodies` ).

Hope it helps anyone in my same situation 🙂
