#transform error

14 messages · Page 1 of 1 (latest)

neon dawn
#

I spawn a sprite and attach a struct to it, then get its translation but the game crashes saying the entity doesn't exist.

pub fn setup_ingame(
    mut commands: Commands,
    asset_server: Res<AssetServer>,
    ingame_setup: ResMut<InGameSetup>,
    mut obstacles: ResMut<Obstacles>,
    mut spawn_ground: ResMut<SpawnGround>,
    mut ground_transform: Query<&mut Transform, With<Ground>>,
    
){
    if ingame_setup.run {
        
        if spawn_ground.run {
            commands.spawn((SpriteBundle {
                transform: Transform::from_xyz(0.0, -378.0, 0.0),
                texture: asset_server.load("ground.png"), 
                ..Default::default()
            },
            Ground,
            ))
            .insert(RigidBody::KinematicVelocityBased);  

            let ground_pos = ground_transform.single_mut();
            let mut x = ground_pos.translation.x;

            spawn_ground.run = false;
        }
    }
}
#

```thread 'Compute Task Pool (1)' panicked at 'called Result::unwrap() on an Err value: NoEntities("bevy_ecs::query::state::QueryState<&mut bevy_transform::components::transform::Transform, bevy_ecs::query::filter::Withgame::Ground>")', src\setup_ingame.rs:27:47
note: run with RUST_BACKTRACE=1 environment variable to display a backtrace
thread 'main' panicked at 'called Option::unwrap() on a None value', C:\Users\eneva.cargo\registry\src\github.com-1ecc6299db9ec823\bevy_tasks-0.9.1\src\task_pool.rs:273:45
error: process didn't exit successfully: target\debug\game.exe (exit code: 101)

#

"spawn_ground.run" is not the thing causing the problem I think

#

I still get the same error when I remove it

lime quartz
#

The entity will only exist after the end of the stage setup_ingame runs in

#

I suspect wherever you're trying to pull this entity's Transform component, it's in a system that runs in this same stage... presumably you're passing the Entity to it via event or through a Resource?

neon dawn
#

I'm trying to spawn the entity then get its transform immediately after that

lime quartz
#

You can reference the Transform you created for spawning the entity

#

But the entity hasn't actually spawned yet, so that Transform won't exist as a component in the World

neon dawn
#

so I should seperate the spawning and the transform in different systems

lime quartz
#

Depends on what you're trying to do, that could certainly work

neon dawn
#

I'm gonna try that one second

lime quartz
#

However I haven't found an instance where I ever needed to do that - any case where you would want to immediately change the component, you could have just changed the component before spawning it with commands

neon dawn
#

Yea that fixed it