Search code examples
bevy

How can I despawn an entity in bevy 0.5.0 that was spawned with commands.spawn_bundle()


This is a very simple question. I already rewrote my code to work with the syntax and other changes, that came with the new version of bevy.

Everything seems to work when compiling, except the despawning of an entity.

I spawn in said entity like:

commands.spawn_bundle(PbrBundle {
            mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
            material: builder_texture.clone(),
            transform: Transform::from_matrix(Mat4::from_scale_rotation_translation(
                Vec3::splat(0.75),
                Quat::from_rotation_x(0.0),
                Vec3::new(0.0, 0.0, 0.0),
            )),
                ..Default::default()
        })
        .insert(controll::BuilderIndicator);

but I'm unable to despawn it like:

fn despawn(
    mut entity: Query<(Entity, controll::BuilderIndicator), With<controll::BuilderIndicator>>,
    mut commands: Commands,
) {
    for (entity, example) in entity.iter_mut() {
        commands.despawn(entity);
    }
}

It returns:

error[E0599]: no method named `despawn` found for struct `bevy::prelude::Commands<'_>` in the current scope
   --> src/controll.rs:120:26
    |
120 |                 commands.despawn(entity);
    |                          ^^^^^^^ help: there is an associated function with a similar name: `spawn`

error: aborting due to previous error

What do I have to do to make it work?


Solution

  • It seems that some methods have been moved to EntityCommands. So you'd have to do: commands.entity(entity).despawn();

    I haven't tested yet.