stride3d/stride · error · InvalidOperationException
Entity on this instance
Error message
Entity on this instance [{GetType().Name}] cannot be null What it means
EntityComponent.EnsureEntity is a protected helper that returns the owning Entity and throws InvalidOperationException when Entity is null. The library throws this because most component APIs (transform access, processor callbacks) only make sense when the component is attached to an Entity. It signals the component is being used while still detached.
Solutions
- Attach the component to an Entity (entity.Components.Add(component)) before using members that depend on EnsureEntity
- Guard with a null check on component.Entity before invoking such members
- If the component should already be attached, check whether it was removed or duplicated (one component instance per entity)
Example fix
// before
var transform = myComponent.EnsureEntity.Transform;
// after
if (myComponent.Entity == null)
entity.Components.Add(myComponent);
var transform = myComponent.EnsureEntity.Transform; Defensive patterns
Strategy: type-guard
Validate before calling
if (component.Entity == null) throw new InvalidOperationException("Component is not attached to an entity yet"); Type guard
static bool IsAttached(Stride.Engine.EntityComponent c) => c?.Entity != null;
Try / catch
try { UseEnsureEntityDependentApi(component); }
catch (InvalidOperationException ex) when (ex.Message.Contains("cannot be null")) { /* attach component or abort */ } Prevention
- Always add components to an Entity before using their logic
- Never invoke component behavior from detached/pooled instances
- After deserialization, verify Entity back-references are set
When it happens
Trigger: Accessing a derived component member or helper that reads EnsureEntity before the component has been added to an Entity's Components collection (or after removal).
Common situations: Calling component methods on a newly constructed component before scene/entity assignment; deserialized components missing their entity back-reference; code moved out of an entity but still invoked.
Related errors
- Cannot add a null component
- This component is already attached to entity
- and cannot both be empty.
- There must be two and only two input values for Int2.
- Indices for Int2 run from 0 to 1, inclusive.
AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14).
Data as JSON: /api/errors/0032aace2a011946.
Report an issue: GitHub.
Appendix: source
Thrown at sources/engine/Stride.Engine/Engine/EntityComponent.cs:47
/// The unique identifier of this component.
/// </summary>
[DataMember(int.MinValue)]
[Display(Browsable = false)]
[NonOverridable]
public Guid Id { get; set; } = Guid.NewGuid();
/// <summary>
/// Gets the entity and throws an exception if the entity is null.
/// </summary>
/// <value>The entity.</value>
/// <exception cref="System.InvalidOperationException">Entity on this instance is null</exception>
[DataMemberIgnore]
protected Entity EnsureEntity
{
get
{
if (Entity == null)
throw new InvalidOperationException($"Entity on this instance [{GetType().Name}] cannot be null");
return Entity;
}
}
internal class Serializer : DataSerializer<EntityComponent>
{
private DataSerializer<Guid> guidSerializer;
/// <inheritdoc/>
public override void Initialize(SerializerSelector serializerSelector)
{
guidSerializer = MemberSerializer<Guid>.Create(serializerSelector);
}
public override void Serialize(ref EntityComponent obj, ArchiveMode mode, SerializationStream stream)
{
var entity = obj.Entity;
View on GitHub (pinned to 96fad776d2)