Remove components from an entity
To remove components from an entity, use the EntityManager
for the World that the entity is in.
Important
Removing a component from an entity is a structural change which means that the entity moves to a different archetype chunk.
From the main thread
You can directly remove components from an entity from the main thread. The following code sample gets every entity with an attached Rotation
component and then removes the Rotation
component.
public partial struct RemoveComponentSystemExample : ISystem
{
public void OnCreate(ref SystemState state)
{
var query = state.GetEntityQuery(typeof(Rotation));
state.EntityManager.RemoveComponent<Rotation>(query);
}
}
From a job
Because removing a component from an entity is a structural change, you can't directly do it in a job. Instead, you must use an EntityCommandBuffer
to record your intention to remove components later.