Download

Can't destroy Actor

This code is used to store items into an inventory, but if the item name already exists then increase the quantity. If this is the case, I want to destroy the pickup. How could I fix this issue of not being able to destroy the actor.
Pickup Code:



void APickup::Interact_Implementation()
{
ARpgGameCharacter* Character = Cast <ARpgGameCharacter>(UGameplayStatics::GetPlayerCharacter(this, 0));

// TODO: use a different unique identifier later to identify items for the key
UInventoryItem* TestInventoryItem = nullptr;
UE_LOG(LogTemp, Warning, TEXT("Start - Pickup"));
if (Stackable)
{
int32 index = Character->TestInventory.IndexOfByPredicate([this](const UInventoryItem* InvItem)
{
return InvItem->Item->ItemName == ItemName;
});

UE_LOG(LogTemp, Warning, TEXT("Pickup: %d"), index);
if (index != INDEX_NONE)
{
UE_LOG(LogTemp, Warning, TEXT("Here"), index);
TestInventoryItem = Character->TestInventory[index];
UE_LOG(LogTemp, Warning, TEXT("%d"), TestInventoryItem->Quantity);
}
}


if (TestInventoryItem == nullptr)
{
TestInventoryItem = NewObject<UInventoryItem>();
TestInventoryItem->Item = this;
Character->TestInventory.Add(TestInventoryItem);
}

TestInventoryItem->Quantity++;
OnPickedUp();
}


Player code:



void ARpgGameCharacter::Interact()
{
if (CurrentInteractable != nullptr)
{
CurrentInteractable->Interact_Implementation();
CurrentInteractable->Destroy();
}
}