Shedule & Run return different results

Why does result differ?

using UnityEngine;
using Unity.Collections;
using Unity.Entities;
using Unity.Jobs;
using Unity.Transforms;

public class ClosureVarCaptureTestSystem : SystemBase
{
    protected override void OnUpdate()
    {
        NativeArray<float> testVar = new NativeArray<float>(1, Allocator.TempJob);

        Entities.ForEach((ref Translation position) =>
        {
            testVar[0] = 1f;

        }).Schedule();
 
        Debug.Log("Test var value:" + testVar[0]);

        testVar.Dispose();
    }
}

returns Test var value:0

Same code with Run() instead of Schedule() returns Test var value:1*.*

Unity 2019.3.15f1, entities 0.11.0-preview.7.

Most likely bacause you log the result before the job executes when using schendule.
Try adding this before the log :

Dependency.Complete();

you should get the same result than run, I think.

1 Like

Yep, that’s it. Thank you!

Also one very important thing to note… It looks like you disabled the jobs debugger. It would have told you about this race condition. It’s in the job menu, it’s a bad idea to turn it off unless you are just trying to profile in the editor.

1 Like

Thank you, a useful note! Unfortunately I do have JobsDebugger enabled and I do not see any notifications neither at compile time nor at runtime. Not sure what I’m doing wrong.

Do you have errors disabled in the console window?

I tried your code and it outputs the expect invalid operation exception for me.

I think info warnings and errors are enabled. E.g. I see warning ‘IJobForEachWithEntity is obsolete’ and also Error and Info badges showing 0. Maybe to delete Library folder and rebuild dependencies and project, to get some additional info is a good idea.

I’ve disabled system with [DisableAutoCreation] while experimenting with this, that’s why I’ve got no errors at runtime!