bevyengine/bevy · error · SpawnError

Invalid id: {0}

Error message

Invalid id: {0}

What it means

thiserror Display message for SpawnError::Invalid: an attempted spawn used an Entity whose id failed validation — typically the wrong generation for a freed index, or an Entity fabricated without the allocator. The wrapped InvalidEntityError supplies the offending id in {0}.

Source

Thrown at crates/bevy_ecs/src/entity/mod.rs:1115

        self.meta
            .iter()
            .filter(|meta| meta.location.is_some())
            .count() as u32
    }

    /// Returns true if there are any entity indices currently spawned.
    /// See the module docs for a more precise explanation of what spawning means.
    pub fn any_spawned(&self) -> bool {
        self.meta.iter().any(|meta| meta.location.is_some())
    }
}

/// An error that occurs when a specified [`Entity`] can not be spawned.
#[derive(thiserror::Error, Debug, Clone, Copy, PartialEq, Eq)]
pub enum SpawnError {
    /// The [`Entity`] to spawn was invalid.
    /// It probably had the wrong generation or was created erroneously.
    #[error("Invalid id: {0}")]
    Invalid(InvalidEntityError),
    /// The [`Entity`] to spawn was already spawned.
    #[error("The entity can not be spawned as it already has a location.")]
    AlreadySpawned,
}

/// An error that occurs when a specified [`Entity`] does not exist in the entity id space.
/// See [module](crate::entity) docs for more about entity validity.
#[derive(thiserror::Error, Debug, Clone, Copy, PartialEq, Eq)]
#[error(
    "The entity with ID {entity} is invalid; its index now has generation {current_generation}."
)]
pub struct InvalidEntityError {
    /// The entity's ID.
    pub entity: Entity,
    /// The generation of the [`EntityIndex`], which did not match the requested entity.
    pub current_generation: EntityGeneration,
}

View on GitHub (pinned to 396ca72708)

Solutions

  1. Obtain Entity ids only from the same World's spawn/reserve APIs
  2. If the id came from an earlier frame, re-fetch it — the entity was likely despawned and its generation advanced
  3. When using reserved entities, finish spawning via the returned Entity command before other use
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/bevy_ecs/src/entity/mod.rs:1115 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of bevyengine/bevy@396ca72708 (2026-08-20). Data as JSON: /api/errors/809d5d88298e7c26. Report an issue: GitHub.