sschmid/Entitas · error · EntityDoesNotHaveComponentException
Cannot get component
Error message
Cannot get component '{_contextInfo.ComponentNames[index]}' from {this}! What it means
Thrown by Entity.GetComponent when the component at the given index does not exist on the entity. Reading a component in Entitas requires that it is present; there is no null-return contract, so a missing component is an exception.
Solutions
- Use the generated has-check: if (entity.hasPosition) { var p = entity.position; }
- Call entity.HasComponent(index) before GetComponent for raw-index access
- Match the group on all components you intend to read (e.g. Matcher.AllOf(...))
- Restructure so removal happens after all readers processed the frame
Example fix
// before
var pos = entity.position; // throws when !hasPosition
// after
if (entity.hasPosition) {
var pos = entity.position;
// use pos
} Defensive patterns
Strategy: type-guard
Validate before calling
if (entity.HasComponent(index)) { var c = entity.GetComponent(index); } Type guard
bool TryGetComponent(Entity e, int index, out IComponent c) { c = e.HasComponent(index) ? e.GetComponent(index) : null; return c != null; } Try / catch
try { var c = entity.GetComponent(index); }
catch (EntityDoesNotHaveComponentException) { c = null; } Prevention
- Use generated properties behind the has-check (if (e.hasX) var x = e.x;)
- Match groups with Matcher.AllOf for all components you read
- Verify system execution order so removals never precede reads
When it happens
Trigger: Calling entity.GetComponent(index) or a generated e.position when entity.hasPosition is false; reading after RemoveComponent; reading in a system that matched a broader group than the component requires.
Common situations: Group matching on one component while reading another; ordering bugs where a removal system runs before the reader; typos confusing similar components.
Understand the failure class
Background: "Not found" and "does not exist" errors: why "Task not found", "No such folder", and "Can't find" fire when a lookup comes back empty — this error's family across 14 libraries.
Related errors
- Cannot remove component
- Cannot replace component
- Cannot destroy !
- EntityIsAlreadyRetainedByOwnerException(_entity, owner)
- EntityIsNotRetainedByOwnerException(_entity, owner)
AI-assisted analysis of sschmid/Entitas@37547d1bd2 (2026-09-14).
Data as JSON: /api/errors/d192d471de1015d5.
Report an issue: GitHub.
Appendix: source
Thrown at src/Entitas/Entity/Entity.cs:215
OnComponentRemoved?.Invoke(this, index, previousComponent);
}
GetComponentPool(index).Push(previousComponent);
}
else
{
OnComponentReplaced?.Invoke(this, index, previousComponent, newComponent);
}
}
/// Returns a component at the specified index.
/// You can only get a component at an index if it exists.
/// The preferred way is to use the
/// generated methods from the code generator.
public IComponent GetComponent(int index)
{
if (!HasComponent(index))
throw new EntityDoesNotHaveComponentException(index,
$"Cannot get component '{_contextInfo.ComponentNames[index]}' from {this}!",
"You should check if an entity has the component before getting it.");
return _components[index];
}
/// Returns all added components.
public IComponent[] GetComponents()
{
if (_componentsCache == null)
{
for (var i = 0; i < _components.Length; i++)
{
var component = _components[i];
if (component != null)
_componentBuffer.Add(component);
}
View on GitHub (pinned to 37547d1bd2)