using a Ijob system to create spawn system with a spawn timer

This has been blowing my mind for the past few days and I tried so many things. I have a GameObject that holds a prefab, spawn count, spawn amount and spawn timer. This object gets converted into an entity with with Game Object Entity script.

I got a job system to work without a hitch for spawning prefabs without a hitch, but its when creating a spawn timer that things get super complicated for me.

Here is the jobs system (faily simple):

using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
using Unity.Burst;

public struct Villager_SpawnJobS : IJobForEachWithEntity<Spawner_FromEntity, LocalToWorld>
    public float DeltaTime;
    public EntityCommandBuffer.Concurrent CommandBuffer;
    public Random random;
    public void Execute(Entity entity, int index, [ReadOnly] ref Spawner_FromEntity spawnerFromEntity, [ReadOnly] ref LocalToWorld location)
        var seed = (uint)(5 + index);
        var rnd = new Unity.Mathematics.Random(seed);
        var spawnTime = spawnerFromEntity.SpawnDelay;
        //spawnTime -= DeltaTime;
        //if (spawnTime <= 0)
        //    spawnTime = 3f;
            for (var s = 0; s < spawnerFromEntity.MaxSpawn; s++)
                var instance = CommandBuffer.Instantiate(index, spawnerFromEntity.Prefab);
                var position = math.transform(location.Value, new float3(rnd.NextInt(-35, 35), 0, rnd.NextInt(-35, 35)));
                CommandBuffer.SetComponent(index, instance, new Translation { Value = position });
        CommandBuffer.DestroyEntity(index, entity);

The bit that I’ve commented out is the spawn time, but it doesn’t work and it makes sense why. The job does the calculation once and then sets the location. The spawn timer gets decremented once, which sets it to about 2.9/2.8 and it will stay there- forever. Never reaching 0. I’m not sure how to approach this considering this same situation happens to me for various things.

You need to parallelize your spawner then you can spawn new entities without freezing the game. In Unity, the most easier way to implement parallelization is through Coroutine.

public class Spawner : MonoBehaviour
    public float DeltaTime;
    public GameObject Prefab;

    public void Start()
        // init stuf...


    public IEnumerator()
        while (true)
            // Spawn the prefab

            yield return new WaitForSeconds(DeltaTime);

Of course, your class needs to inherit from MonoBehaviour in order to use Coroutines. If you don’t want to work with Unity Coroutines, you need to use C# Threads (Thread Classe (System.Threading) | Microsoft Learn) but you will have to manage mutex on your own. Also, note that Unity does not provide a way to communicate between threads, so you will have to manage it on your own with some kind of mechanism (struct/queue), you can read more on multi-threading in Unity solutions:

You can also give a catch to new Unity Entity Component System (ECS)