#Beginner Question. How to remove component with ECB?

1 messages · Page 1 of 1 (latest)

tawny vector
#

I have a component that spawns a gameobject and I only want to do it once. I'm removing the component with an ECB, but looking at the entity hierarchy editor window, the system is spawning the object every frame.

public void OnUpdate(ref SystemState state)
    {
        var ecb = SystemAPI.GetSingleton<EndSimulationEntityCommandBufferSystem.Singleton>();
        var commands = ecb.CreateCommandBuffer(state.WorldUnmanaged);
        
        foreach (RefRW<HeroSpawnerComponent> spawner in SystemAPI.Query<RefRW<HeroSpawnerComponent>>())
        {
            Entity entity = state.EntityManager.Instantiate(spawner.ValueRO.Prefab);
            state.EntityManager.SetComponentData(entity, LocalTransform.FromPosition(Vector3.zero));
            commands.RemoveComponent<HeroSpawnerComponent>(entity);
        }
    }
solemn lagoon
#

You’re removing hero spawner from your spawned entity not from spawner entity

#

Use .WithEntity for your query and pass this entity to ecb