Download

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.