2

Description

I'm trying to implement trigger logic when the player faced the trigger I should remove the UI element from the screen.

Spawning the trigger point

/// Create a trigger point and when the user faced with
///  it I'll mark the tutorial as `in-progress` and
///  remove it when the collision between tutorial
///  and player is stopped
commands
  .insert(Sensor(true))
  .insert(Collider::cuboid(8.0, 8.0))
  .insert(ActiveEvents::COLLISION_EVENTS)
  .insert_bundle(SpriteBundle {
    sprite: Sprite {
      color: Color::rgb(0.1, 0.1, 0.1),
      custom_size: Some(Vec2::new(16.0, 16.0)),
      ..Default::default()
    },
    transform: *tutorial_transform,
    ..Default::default()
  })

  // Tutorial is a component which I'll filter as `tutorial_entity`
  .insert(Tutorial);

Create a UI

commands
  .spawn_bundle(NodeBundle {
    ///
  })

  /// Trying to bind UI element with `Tutorial` entity
  ///  to remove it from the screen when the user faced with collider
  .insert(Parent(tutorial_entity))

When the user faced collision

// I want to despawn all children UI elements that are linked with this non-UI element
commands.entity(tutorial_entity).despawn_recursive()

Error

I've got an error and no UI on the screen at all

Styled child in a non-UI entity hierarchy. You are using an entity with UI components as a child of an entity without UI components, results may be unexpected

Question

Do you know how to link a non-UI element with a UI element to remove the non-UI element and remove all linked UI elements with it?

MadMed677
  • 63
  • 3

2 Answers2

0

I don't know if its still relevant but you could always just create a separate UI entity and add your own reference component. I guess the for structurings sake i would create ( for example ) a UILinkComponent(pub Entity) and attach it to the world entity.

EMP
  • 31
  • 3
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 08 '22 at 09:53
0

Pretty sure you shouldn't use Parent for that kind of relationship. The hierarchy is typically used for spatial relationships.

You should instead create a new component that stores the other Entity. You can still destroy it in the same way, but this way it isn't part of the hierarchy so the other UI elements don't get confused.

kmdreko
  • 42,554
  • 6
  • 57
  • 106