I am struggling to Destroy() weapons attached to an AI Character once the AI Character has been killed. The AI Character can be destroyed, but the weapons spawned by that character persist in the world. I have an Actor Gun Class, and then create different blueprint variations of guns, which the AI Character spawns.
The setup code to spawn the guns works well:
void APlayerCharacterBase::BeginPlay()
{
Super::BeginPlay();
for (TSubclassOf<AGunBase> GunClass : GunClasses)
{
Guns.Add(GetWorld()->SpawnActor<AGunBase>(GunClass));
}
for (int32 i = 0; i < Guns.Num(); i++)
{
Guns[i]->FindComponentByClass<UStaticMeshComponent>()->SetVisibility(false, true);
Guns[i]->SetOwner(this);
Guns[i]->AttachToComponent(GetMesh(), FAttachmentTransformRules::KeepRelativeTransform, TEXT("WeaponSocketTPV"));
}
and then later to destroy the AI Character:
void APlayerCharacterBase::HandleDestruction()
{
Guns[0]->Destroy(); //trying to implicitly destroy the 1st gun and it doesn't work. Returns false
DetachFromControllerPendingDestroy(); // This works
Destroy(); // This works in destroying the AI Character
}
I’ve searched the forums and struggled to find an answer.