IJobEntity with a managed component throwing an error saying it is writing to the EntityManager when it is not

I have a managed component, BulletTrailVFX, with various managed types.

BulletTrailVFX Definition
public class BulletTrailVFX : IComponentData
{
    public VisualEffect VisualEffect;
    public ExposedProperty SpawnBufferProperty;
    public ExposedProperty SpawnBufferCountProperty;
    public ExposedProperty BulletSpeedProperty;

    public int SpawnBufferInitialCapacity;
    public float BulletSpeed;

    public GraphicsBuffer SpawnGraphicsBuffer;
    public List<IdPositionAngle> SpawnData = new List<IdPositionAngle>();
}

And the following job that operates on the component:

Job Definition
public partial struct WriteBulletPositionsToVFXBufferJob : IJobEntity
{
    [ReadOnly] public NativeArray<IdPositionAngle> PositionArray;
    public float BulletSpeed;

    public void Execute(BulletTrailVFX vfxSpawn)
    {
        vfxSpawn.BulletSpeed = BulletSpeed;

        foreach (IdPositionAngle posAngle in PositionArray)
        {
            vfxSpawn.SpawnData.Add(posAngle);
        }
    }
}

And I schedule the job with this code:

Job Scheduling Code
EntityQuery vfxQuery = SystemAPI.QueryBuilder()
    .WithAllRW<BulletTrailVFX>()
    .Build();

new WriteBulletPositionsToVFXBufferJob
{
    PositionArray = positionsArray,
    BulletSpeed = pmg.BaseBulletSpeed
}.Schedule(vfxQuery);

positionsArray.Dispose(state.Dependency);

There is only one system that schedules this job, and this job/system is the only place that this component is touched (I have temporarily removed the code that consumes the data that this job writes in an attempt to narrow down the problem). In playmode I get this error:

InvalidOperationException: The previously scheduled job PlasmaMachineGunSystem:WriteBulletPositionsToVFXBufferJob writes to the Unity.Entities.EntityManager WriteBulletPositionsToVFXBufferJob.JobData.__EntityManager. You must call JobHandle.Complete() on the job PlasmaMachineGunSystem:WriteBulletPositionsToVFXBufferJob, before you can write to the Unity.Entities.EntityManager safely.

From my understanding, the job shouldn’t be interacting with the EntityManager at all since I’m not adding, removing, or setting any components anywhere, and even if it were changing values I still don’t understand why it would give me that error when I’m not interacting with that component anywhere else (much less from a context that would also modify it or the EntityManager associated with this job before it would complete), unless I’ve fundamentally misunderstood how job scheduling works.

If someone could point me to what I might be doing wrong or some next steps for troubleshooting I would really appreciate it. Thanks!

1 Like

IJobEntity codegen automatically produces a usage of EntityManager in order to support accessing managed components in a job. Also, you’re not able to schedule jobs with managed components, only run them directly and immediately on the main thread.

More details:
https://docs.unity3d.com/Packages/com.unity.entities@1.3/manual/iterating-data-ijobentity.html#execute-parameters

1 Like

Ah thank you! That explains the issue. I’ll have to figure out another way to deal with it then