diff --git a/crates/bevy_render/src/view/visibility/mod.rs b/crates/bevy_render/src/view/visibility/mod.rs index b018103afc6c5..2a465c55273e5 100644 --- a/crates/bevy_render/src/view/visibility/mod.rs +++ b/crates/bevy_render/src/view/visibility/mod.rs @@ -310,10 +310,10 @@ fn visibility_propagate_system( let is_visible = match visibility { Visibility::Visible => true, Visibility::Hidden => false, - Visibility::Inherited => match parent { - None => true, - Some(parent) => visibility_query.get(parent.get()).unwrap().1.get(), - }, + // fall back to true if no parent is found or parent lacks components + Visibility::Inherited => parent + .and_then(|p| visibility_query.get(p.get()).ok()) + .map_or(true, |(_, x)| x.get()), }; let (_, mut inherited_visibility) = visibility_query .get_mut(entity) @@ -721,6 +721,24 @@ mod test { assert!(!q.get(&world, id4).unwrap().is_changed()); } + #[test] + fn visibility_propagation_with_invalid_parent() { + let mut world = World::new(); + let mut schedule = Schedule::default(); + schedule.add_systems(visibility_propagate_system); + + let parent = world.spawn(()).id(); + let child = world.spawn(VisibilityBundle::default()).id(); + world.entity_mut(parent).push_children(&[child]); + + schedule.run(&mut world); + world.clear_trackers(); + + let child_visible = world.entity(child).get::().unwrap().0; + // defaults to same behavior of parent not found: visible = true + assert!(child_visible); + } + #[test] fn ensure_visibility_enum_size() { use std::mem;