Download

Destroy AActor doesnt work

Hello,

iam new in UE4 and i would like to ask for help please.

In my ACharacter is spawning AActor from class


ABox* NewBox= GetWorld()->SpawnActor<ABox>(ABox::StaticClass(), Data.Location, Data.Rotation);

then add to array - TArray<ABox*> Boxes:


Boxes.Add(NewBox);

and then call Destroy():



ABox* NewBox= Boxes[0];
NewBox->Destroy();


and nothing. Destroy was OK (true), but box is stil in world.

I tried GetWorld()->DestroyActor(NewBox); and nothing too…

If i use line trace and GetComponent() from FHitResult it works. If i save to array UPrimitiveComponent instead of ABox* it doesnt work…

Is possible to destroy actro from TArray please?

Thank you

I don’t really see any errors in your code, so in theory it should work. Are you sure that you are destroying the right box? Also, is your Boxes array marked with UPROPERTY() ? You need it because you are storing pointers in the array, and these pointers will be removed by the garbage collector if not marked.

Thank you for response.

Yes i have -

UPROPERTY()
TArray<ABox*> Boxes;

and still without effect :confused:

Static mesh component for Box is:

StaticMesh is basic Cube from engine

I tried set SetMobility for Movable - nothing happend



StaticMeshComponent = NewObject<UStaticMeshComponent>(UStaticMeshComponent::StaticClass());
StaticMeshComponent->SetStaticMesh(StaticMesh);
StaticMeshComponent->SetCollisionObjectType(ECollisionChannel::ECC_WorldDynamic);
StaticMeshComponent->SetWorldLocationAndRotation(RootComponent->GetOwner()->GetActorLocation(), RootComponent->GetOwner()->GetActorRotation());
StaticMeshComponent->SetMobility(EComponentMobility::Stationary);
StaticMeshComponent->RegisterComponentWithWorld(GetWorld());
FAttachmentTransformRules rules(EAttachmentRule::KeepWorld, EAttachmentRule::SnapToTarget, EAttachmentRule::SnapToTarget, false);
StaticMeshComponent->AttachToComponent(RootComponent, rules);


Any advice please?

Ok, i dont know if it is a good solution, but i created method in AActor class “DestroyInstance” with content:

StaticMeshComponent->DestroyComponent();
Destroy();

and it works

it is good?:slight_smile: thank you