Destroy Component does not work

I am trying to destroy child components of an actor blueprint, which are of a certain height and when DestroyComponent is called, they remain on the screen. I know the function is called after debugging so it is supposed to work. The actors are made of 3 or 4 child Spheres, who have box colliders. This is my script:

I have tried casting to StaticMeshComponent before calling DestroyComponent but it did not work, and I have tried with checking “Include All Descendants” while enumerating the child components and it did not work either.

Try storing the resultant arrays (GetAllActorsOfClass) and (GetChildrenComponents) locally before entering each Loop. In your current logic, each Loop iteration will call the nodes “GetChildrenComponents” or GetAllActorsOfClass, this can lead to unexpected results cause they are being modified on each iteration.

Another thing you should consider is that each DestroyComponent call could be rearranging the Children Array. You can try a reverse Loop, starting from the last index and ending on first.

OK the problem I found out was I was calling DestroyComponent from outside the actor class, I made a function that destroys the children from inside the actor class and it partially worked. There are still components not being destroyed. Does calling DestroyComponent remove the component from the array? Because the array seems to stay the same in the loop.

2 Likes

This works, thanks!

I know this thread is quite old, but I exactly had the same issue…

It’s not possible to destroy an ActorComponent from outside of the owning actor from Blueprints. So a huge question came into my mind, why can I call this frunction, from the outside of an actor if it does not do anything (it’s a public method :slight_smile:)?!?
And in the log I found “May not destroy component”, so it is destroyed or not? :slight_smile:

Looking deeper into the engine code it’s clear that the component is not destroyed if “bAllowAnyoneToDestroyMe” is not set to true (1) or the method is not called in the context of the owning actor.

Sadly “bAllowAnyoneToDestroyMe” is not exposed to Blueprints. :frowning:

So I decided to create a proper solution to force the destruction within a BlueprintFunctionLibrary.
Here is the code: Force Unreal Engine Actor Component Destruction from Blueprints · GitHub.

8 Likes

Amazing!

1 Like

but where do I paste your code :sweat_smile:
actually when I play test the actor are gone
just still show the annoyed warning

Without any further code snippts or the steps you made it is pretty hard to give a good advice.

hi you cannot destroy component from outside, but you can set its visibility and its collision setting. for example, instead of destroying you can simply make it invisible and set turn its collision off, and play fx there outside the actor itself. it works really well

this is the best answer!

If you create the component from C++ you can also set bAllowAnyoneToDestroyMe to true so you can use the Destroy function on it from anywhere

	UMyActorComponent* MyComponent = NewObject<UMyActorComponent>(this);
	MeshComponent->bAllowAnyoneToDestroyMe = true;

You can also set it directly in the constructor for the actor component

UMyActorComponent::UMyActorComponent()
{
	bAllowAnyoneToDestroyMe = true;
}