diff --git a/crates/bevy_hierarchy/src/child_builder.rs b/crates/bevy_hierarchy/src/child_builder.rs index e759121daa63d..67f16c555112c 100644 --- a/crates/bevy_hierarchy/src/child_builder.rs +++ b/crates/bevy_hierarchy/src/child_builder.rs @@ -1,7 +1,4 @@ -use crate::{ - prelude::{Children, Parent}, - HierarchyEvent, -}; +use crate::{Children, HierarchyEvent, Parent}; use bevy_ecs::{ bundle::Bundle, entity::Entity, @@ -68,12 +65,19 @@ fn update_old_parents(world: &mut World, parent: Entity, children: &[Entity]) { fn remove_children(parent: Entity, children: &[Entity], world: &mut World) { let mut events: SmallVec<[HierarchyEvent; 8]> = SmallVec::new(); - for child in children { - world.entity_mut(*child).remove::(); - events.push(HierarchyEvent::ChildRemoved { - child: *child, - parent, - }); + if let Some(parent_children) = world.get::(parent) { + for &child in children { + if parent_children.contains(&child) { + events.push(HierarchyEvent::ChildRemoved { child, parent }); + } + } + } else { + return; + } + for event in &events { + if let &HierarchyEvent::ChildRemoved { child, .. } = event { + world.entity_mut(child).remove::(); + } } push_events(world, events);