diff --git a/crates/bevy_ui/src/layout/mod.rs b/crates/bevy_ui/src/layout/mod.rs index 43d767acde..154b85cb5e 100644 --- a/crates/bevy_ui/src/layout/mod.rs +++ b/crates/bevy_ui/src/layout/mod.rs @@ -190,9 +190,6 @@ pub fn ui_layout_system( } scale_factor_events.clear(); - // clean up removed nodes - ui_surface.remove_entities(removed_components.removed_nodes.read()); - // clean up removed cameras ui_surface.remove_camera_entities(removed_components.removed_cameras.read()); @@ -217,6 +214,9 @@ pub fn ui_layout_system( } } + // clean up removed nodes after syncing children to avoid potential panic (invalid SlotMap key used) + ui_surface.remove_entities(removed_components.removed_nodes.read()); + for (camera_id, camera) in &camera_layout_info { let inverse_target_scale_factor = camera.scale_factor.recip();