I’m trying to attach an actor to a socket on my character. I can see through debug messages that the actor is being created and attached to the socket, but I cannot see the actor on my character afterwards.
In short I have a character class “MainCharacter”. This character class contains a pointer to UWeaponComponent which is a class that inherits from UActorComponent. UWeaponComponent has an EquipWeapon function in which I pass my weapon actor and attach it to my MainCharacter’s RightHandSocket on its mesh:
void UWeaponComponent::EquipWeapon(AWeapon* NewWeapon)
{
if (Character == nullptr || NewWeapon == nullptr) return;
EquippedWeapon = NewWeapon;
EquippedWeapon->SetOwner(Character);
const USkeletalMeshSocket* HandSocket = Character->GetMesh()->GetSocketByName(FName("RightHandSocket"));
if (HandSocket) {
HandSocket->AttachActor(EquippedWeapon, Character->GetMesh());
if (GEngine) GEngine->AddOnScreenDebugMessage(-1, 15.f, FColor::Green, FString(TEXT("Weapon Equipped")));
}
}
For now I am calling EquipWeapon in the BeginPlay() function of MainCharacter to equip a weapon at the start of the game.
if (Weapon && BPWeapon) {
if (GEngine) GEngine->AddOnScreenDebugMessage(-1, 15.f, FColor::Green, FString(TEXT("Equipping Weapon")));
Weapon->EquipWeapon(NewObject<AWeapon>(this, this->BPWeapon));
}
I create the weapon as I pass it into the EquipWeapon function. BPWeapon is a reference to the blueprint that has AWeapon as a parent. I’m setting BPWeapon to this blueprint in my MainCharacter blueprint. In my weapon blueprint I have set the mesh to my weapon mesh and can see it clearly in the blueprint editor viewport.
I’ve also created the “RightHandSocket” in my skeletal mesh, set the preview asset to the desired mesh, and it looks correct.
Both debug messages are printed out, so the weapon seems to be getting created properly and attached to the socket, but the mesh for the weapon actor is not visible in game. Any ideas what I’m doing wrong here?
I’m following Stephens Unreal Engine 5 multiplayer shooter course if that helps. He calls EquipWeapon after a player input when the weapon is on the ground, but I just want my character to have the weapon immediately.
Thanks!