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;
[BurstCompile]
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.