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.
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 )?!?
And in the log I found “May not destroy component”, so it is destroyed or not?
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.
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
i have same issue, even inside the actor when destroying and recreating components (static meshes and text render), it works 2 or 3 times but then i get my array of components filled with unknown class components, i tried to force garbage collect but i still get my old components stacked with the new ones that i cannot destroy because they are unknown. Maybe the for each loop cause this, i’ve seen that sometime a component is passed without being properly destroyed. i will try with a Do N or or a function that wil force destroy any classes. in my situation i also can destroy the actor completly and recreate it.
Thanks if you have more infos on that kind of issues