What should happen with attached components when calling DestroyComponent?

I have a blueprint that only has one predefined component (an Arrow) and then the Construction Script generates a lot of additional components using functions like AddSceneComponent, AddStaticMeshComponent and AddChildActorComponent. All of these use manual attachment (AttachTo) and they are all attached to the Arrow, hierarchically. So the Arrow’s children are 4 SceneComponents, but underneath them there are a lot of components.

I am trying to write a function to destroy all of these (in preparation for re-creating them). Naively, I’d think this should work:

I checked in Debug and GetChildrenComponents returns the right stuff (4 SceneComponents), but after running this something weird happens. Some of the underlying components are deleted, some are not. Many are re-attached to Arrow. Actually, calling GetNumChildrenComponents immediately after this returns 15! Weirder yet, the 4 SceneComponents are essentially identical (save for a different transform) yet each is partially deleted differently!

I couldn’t find any documentation note hence my question: if component C is attached to B which is attached to A, what should happen with C when I call DestroyComponent on B?

I can achieve my goal by doing this (essentially keep looking for children and destroying them in a loop):

But this feels wrong :). Can anyone explain? Is this a bug?

im not entirely show but maybe you would have to destroy component C in your scenario before component B. the working script to me is using a for loop to loop to the lowest component hierarchically, C in your scenario, finishing to hit the while loop becuase it hasnt destroyed all the components yet.
This is just an idea however.

Thanks! I thought of doing something like that too. But I’d be interested in what the correct behavior is supposed to be. In my case some Cs are destroyed but others aren’t and I’m curious why.

do you mean you have in your hierarchy a root component and say multiple children called B and they in turn have a child say C. are you saying here that some of the C’s are destroyed from some B’s but not all.

Yes. I actually have 4 almost identical subtrees under A, so B1, B2, B3, B4… each with many C children. B3 is completely deleted (with all children) B1 is partially deleted (some children), B2 has no children deleted. It’s really strange :stuck_out_tongue: I can try to create a simpler repro but I figured I’d ask what should happen first.

Try looking at each components documentation if there is any. it might tell you why they behave like that.