AttachTo not working on USphereComponent

I have a chunk of code that creates a point light and USphereComponent, grabs a reference to the ACharacter’s skeletal mesh, and attempts to attach both components to a socket I’ve created on the mesh:

UMNHitbox::UMNHitbox(){
	UPointLightComponent* pointLight = CreateDefaultSubobject<UPointLightComponent>(FName(TEXT("TestLight")));
	USphereComponent* hitSphere = CreateDefaultSubobject<USphereComponent>(FName(TEXT("HitSphere")));
	hitSphere->SetSphereRadius(25);

	ACharacter* myCharacter = Cast<ACharacter>(GetOwner());

	if (myCharacter != nullptr)
	{
		if (myCharacter->GetMesh() != nullptr)
		{
			GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, "Sphere moved!");
			hitSphere->AttachTo(myCharacter->GetMesh(), "RightHandHB_Socket");
			pointLight->AttachTo(myCharacter->GetMesh(), "RightHandHB_Socket");
		}
	}
}

Both components are created properly, and viewable in edit mode, but they don’t snap to the socket when the game loads and the constructor executes- both components hover below and to the left of the character. I took a screenshot, the socket they’re attached to is in the middle of the character’s right hand, but they always hold position below and to the left of the model as depicted. Is there something obvious I’m doing wrong?

Hmm, you’re thinking something like this, right?

hitSphere->AttachTo(myCharacter->GetMesh(), "RightHandHB_Socket", EAttachLocation::SnapToTarget);

I’m afraid that doesn’t work… both objects remain in the same location, regardless of whether I include the SnapToTarget.

Hi Hinoue,

Try adding the 3rd argument to enforce snapping. EAttachLocation::SnapToTarget

AttachTo(myCharacter->GetMesh(), "RightHandHB_Socket", EAttachLocation::SnapToTarget)

Hope that Helps!

For a sanity check, try :

hitSphere->DetachFromParent();

After you cast for MyCharacter

Hmm, adding DetachFromParent(); doesn’t change anything… I initially thought whatever its position from the character was at start is what it was sticking to, but it looks like it’s snapping somewhere specific- even if I move the ACharacter around and have him start directly above the origin (which is where his components seem to spawn automatically), the sphere and light always revert to the same spot forward and left of the mesh.

So I feel stupid- I made myCharacter a public variable, and it looks like GetOwner isn’t finding the character at all: it remains “none” in the editor after the game loads.

I’m not sure why it’s failing, though: the component is attached to a character, so component->GetOwner(); with the proper cast should find it.

Hmmm, it sounds like its not getting the socket location correctly. I would suggest:

  1. Triplecheck naming, case sensitive

  2. create a “FName AttachPointName” to pass in instead of the char array.

  3. Draw a debug Cross/sphere by at the location GetMesh()->GetSocketLocation(Name);

  4. Make sure you are doing this in the PostInitialize function call and not before!

Those are my suggestions, Good luck!

I think the issue is that it isn’t getting the reference to the character at all, which it should- AActor* testAct = this->GetOwner() correctly returns the character, which is of type MyProjectCharacter, which in turn inherits from ACharacter, so shouldn’t my GetOwner to the same object with a cast to ACharacter return the correct object?