#Error when creating my job

1 messages · Page 1 of 1 (latest)

sweet brook
#

I am trying to run a job after converting a Vector3[] to a NativeArray<float3> but i keep getting an error:

InvalidOperationException: The UNKNOWN_OBJECT_TYPE TranslateJob._data has not been assigned or constructed. All containers must be valid when scheduling a job.

The error does not make sense to me.

This is my code setup:

//translate every vertex by the given translation
public static void Translate(Vector3[] vertices, in float3 translate)
{
    unsafe
    {
        //get pointer of vertices
        fixed (Vector3* ptr = vertices)
        {
            //convert pointer to native array
            var arr = NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray<float3>(ptr, vertices.Length, Allocator.Temp);
            //create job

            var job = new TranslateJob(arr, translate);
            var handle = job.Schedule(arr.Length, 64);
            handle.Complete();

            //copy data back to vertices
            UnsafeUtility.MemCpy(ptr, arr.GetUnsafeReadOnlyPtr(), vertices.Length * sizeof(Vector3));

            //dispose native array
            arr.Dispose();
        }
    }
}

My job looks like this:

struct TranslateJob : IJobParallelFor
{
    NativeArray<float3> _data;
    readonly float3 _translation;
    public TranslateJob(in NativeArray<float3> data, in float3 translate)
    {
        _data = data;
        _translation = translate;
    }
    public void Execute(int index) => _data[index] += _translation;
}
faint sand
#

when you do ConvertExistingDataToNativeArray

#

you also need to call the safety method part

#

let me load up rider and find the code

#

something like

#
    var arr = NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray<float3>(ptr, vertices.Length, Allocator.Temp);
#if ENABLE_UNITY_COLLECTIONS_CHECKS
    NativeArrayUnsafeUtility.SetAtomicSafetyHandle(ref arr, AtomicSafetyHandle.Create());
#endif```
#

i'd usually use AtomicSafetyHandle.GetTempMemoryHandle() instead of Create() and try that first

#

but i think it might have issues if you're passing the array to a job

sweet brook
#

what does that do exactly

#

seems create works but temp doesn't

#

not entirely sure what its doing but at least it works

#

wish unity docs would've explained it i'd have never guessed it

faint sand
#

It sets up the safety handle in the array

sweet brook
#

is safety checks debug specific ? removed at build ?

#

i noticed i can turn off safety checks in jobs menu

faint sand
#

you can't turn them off inside the editor

#

but they are always disabled in builds

sweet brook
#

so is the ENABLE_UNITY_COLLECTIONS_CHECKS really necessary ?

faint sand
#

yes

#

otherwise you will get a compile error

#

when you make a build

#

because safety doesn't exist anymore

sweet brook
#

how did you figure all this out

#

the docs is not well explained on this

faint sand
#

i've been using entities for 5.5 years

#

you kind of learn things

#

documentation didn't even exist when i started so it's a good thing i learn best from reading source code

sweet brook
#

fair enough ! i hope they update it one day 😄

#

the docs do lack a lot of info