{"record":{"id":"d8148f970aebeb2c","repo":"bevyengine/bevy","slug":"the-entity-with-id-entity-is-invalid-its-index","errorCode":null,"errorMessage":"The entity with ID {entity} is invalid; its index now has generation {current_generation}.","messagePattern":"The entity with ID (.+?) is invalid; its index now has generation (.+?)\\.","errorType":"exception","errorClass":"InvalidEntityError","httpStatus":null,"severity":"error","filePath":"crates/bevy_ecs/src/entity/mod.rs","lineNumber":1125,"sourceCode":"    }\n}\n\n/// An error that occurs when a specified [`Entity`] can not be spawned.\n#[derive(thiserror::Error, Debug, Clone, Copy, PartialEq, Eq)]\npub enum SpawnError {\n    /// The [`Entity`] to spawn was invalid.\n    /// It probably had the wrong generation or was created erroneously.\n    #[error(\"Invalid id: {0}\")]\n    Invalid(InvalidEntityError),\n    /// The [`Entity`] to spawn was already spawned.\n    #[error(\"The entity can not be spawned as it already has a location.\")]\n    AlreadySpawned,\n}\n\n/// An error that occurs when a specified [`Entity`] does not exist in the entity id space.\n/// See [module](crate::entity) docs for more about entity validity.\n#[derive(thiserror::Error, Debug, Clone, Copy, PartialEq, Eq)]\n#[error(\n    \"The entity with ID {entity} is invalid; its index now has generation {current_generation}.\"\n)]\npub struct InvalidEntityError {\n    /// The entity's ID.\n    pub entity: Entity,\n    /// The generation of the [`EntityIndex`], which did not match the requested entity.\n    pub current_generation: EntityGeneration,\n}\n\n/// An error that occurs when a specified [`Entity`] is certain to be valid and is expected to be spawned but is not spawned yet.\n/// This includes when an [`EntityIndex`] is requested but is not spawned, since each index always corresponds to exactly one valid entity.\n#[derive(thiserror::Error, Debug, Clone, Copy, PartialEq, Eq)]\npub struct EntityValidButNotSpawnedError {\n    /// The entity's ID.\n    pub entity: Entity,\n    /// The location of what last despawned the entity.\n    pub location: MaybeLocation<&'static Location<'static>>,\n}","sourceCodeStart":1107,"sourceCodeEnd":1143,"githubUrl":"https://github.com/bevyengine/bevy/blob/396ca727080776bd313bb892423b7d94e03b81b4/crates/bevy_ecs/src/entity/mod.rs#L1107-L1143","documentation":"Display message for the invalid-entity error (used by SpawnError::Invalid and friends): the Entity handle's generation is stale relative to the allocator — its index now holds a newer generation, meaning the referenced entity was despawned (or never existed at that generation). The message reports both the stale handle and the current generation.","triggerScenarios":"Thrown at crates/bevy_ecs/src/entity/mod.rs:1125 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Drop stale Entity handles and re-query for the entity by components or relationships","Validate handles with Entities::contains or Query-based checks before use","Track despawn events (on_remove hooks or observers) to invalidate stored ids proactively"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"396ca727080776bd313bb892423b7d94e03b81b4","analyzedAt":"2026-08-20T16:12:39.808Z","contentChangedAt":"2026-08-20T16:12:39.808Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}