bevyengine/bevy · error · GetEntityMutByIdError

the `Component` could not be found

Error message

the `Component` could not be found

What it means

GetEntityMutByIdError::ComponentNotFound is returned by UnsafeEntityCell::get_mut_by_id when the ComponentId is valid and known to the world, but this particular Entity does not have that component in its archetype/table. It is the by-id equivalent of world.get_mut::<T>(entity) returning None: entity alive, component absent.

Source

Thrown at crates/bevy_ecs/src/world/unsafe_world_cell.rs:1229

                .entities()
                .entity_get_spawned_or_despawned_unchecked(self.entity)
                .1
        }
    }
}

/// Error that may be returned when calling [`UnsafeEntityCell::get_mut_by_id`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
pub enum GetEntityMutByIdError {
    /// The [`ComponentInfo`](crate::component::ComponentInfo) could not be found.
    #[error("the `ComponentInfo` could not be found")]
    InfoNotFound,
    /// The [`Component`] is immutable. Creating a mutable reference violates its
    /// invariants.
    #[error("the `Component` is immutable")]
    ComponentIsImmutable,
    /// This [`Entity`] does not have the desired [`Component`].
    #[error("the `Component` could not be found")]
    ComponentNotFound,
}

impl<'w> UnsafeWorldCell<'w> {
    #[inline]
    /// # Safety
    /// - the returned `Table` is only used in ways that this [`UnsafeWorldCell`] has permission for.
    /// - the returned `Table` is only used in ways that would not conflict with any existing borrows of world data.
    unsafe fn fetch_table(self, location: EntityLocation) -> Option<&'w Table> {
        // SAFETY:
        // - caller ensures returned data is not misused and we have not created any borrows of component/resource data
        // - `location` contains a valid `TableId`, so getting the table won't fail
        unsafe { self.storages().tables.get(location.table_id) }
    }

    #[inline]
    /// # Safety
    /// - the returned `ComponentSparseSet` is only used in ways that this [`UnsafeWorldCell`] has permission for.

View on GitHub (pinned to 396ca72708)

Solutions

  1. Handle the None/Err case as normal control flow — this API returns Result<Option<_>, _> precisely so absence is not exceptional.
  2. Pre-check with the typed API world.get::<T>(entity).is_some() or cell.get_by_id(id).is_some().
  3. Ensure removal has not run: schedule the mutating access before the removing system, or apply/flush commands deliberately.
  4. If the component is expected, verify the entity actually spawned the bundle containing it.

Example fix

// before
let mut c = cell.get_mut_by_id(id).unwrap().unwrap(); // ComponentNotFound

// after
match cell.get_mut_by_id(id) {
    Ok(Some(mut c)) => { /* mutate */ }
    Ok(None) => debug!("entity lacks component {id:?}"),
    Err(GetEntityMutByIdError::ComponentNotFound) => unreachable!(),
    Err(e) => debug!("{e}"),
}
Defensive patterns

Strategy: try-catch

Validate before calling

let has = cell
    .world()
    .get_by_id(cell.id(), component_id)
    .is_some();

Try / catch

match cell.get_mut_by_id(component_id) {
    Ok(Some(mut c)) => { /* mutate */ }
    Ok(None) => { /* entity lacks component: skip */ }
    Err(GetEntityMutByIdError::ComponentNotFound) => unreachable!(),
    Err(e) => { /* InfoNotFound / ComponentIsImmutable handling */ }
}

Prevention

When it happens

Trigger: Calling get_mut_by_id(id) for a component the entity never had, had removed earlier in the frame (removal commands already flushed), or that lives only on entities matched by a different query; also after the component was swapped out via insert of a different bundle.

Common situations: Per-component dynamic access in unsafe code paths (relationship targets, dev-tools); assuming required components exist because a query elsewhere filters on them; races between removal observers and later by-id access in the same frame.

Related errors


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