How can I detach actor from certain Socket?

I think I really messed something up:


void UGunItem::Use(class AShooterCharacter* Character)
{

if (GunClass)
{
if (Gun != nullptr)
{
Character->DetachFromActor(FDetachmentTransformRules::KeepRelativeTransform);
UE_LOG(LogTemp, Warning, TEXT("Karabin umar"));
}
Gun = GetWorld()->SpawnActor<ABasePawn>(GunClass);
if (Gun)
{

Gun->AttachToComponent(Character->GetMesh(), FAttachmentTransformRules::KeepRelativeTransform, TEXT("WeaponSocket"));
Gun->SetOwner(Character);
Character->InitalizePointer(Gun);
}
}

}

This code just spawns weapon. I want to detach last weapon before spawning new. Im trying to do this on line 8.

I have no idea what is going on here. So, currently you are detaching the character (which doesn’t really make sense), and also you are spawning a gun casted to ABasePawn? Is the “gun” a character/pawn?

I wanted to detach current gun and spawn new one. Spawning works, but first part is not right.

So how can I despawn or hide previous gun?

You can destroy the actor.

Thanks. One more question. Is destroying worse than hiding if there’s a chance that same mesh will be used later?
For example:
I’ve got two weapons and I can choose freely.

It very much depends on the project. Hiding the weapon is totally viable as well, as long as you only have a couple of weapons in the game. But if you want to have lots of guns, or customizable guns, I think that spawning and destroying weapons is a better approach.

Ok, thank you.