- Home /
Unity Jobs Error - "Job can only create Temp memory"
Hello, I am trying to create the following struct inside a job and get the above error.
public struct JobSafeHeightMap {
//Convert 2D array into 1D, we will have to convert back.
public NativeArray<float> array;
public readonly int size1;
public readonly int size2;
public readonly bool complete;
public JobSafeHeightMap(float[,] array, int size1, int size2, bool complete) {
this.array = new NativeArray<float>(size1 * size2, Allocator.TempJob);
for(int i = 0; i < size1; i++) {
for(int j = 0; j < size2; j++) {
this.array[i * size1 + j] = array[i, j];
}
}
this.size1 = size1;
this.size2 = size2;
this.complete = complete;
}
public float[,] GetArray() {
float[,] returnFloat = new float[size1, size2];
for(int i = 0; i < size1; i++) {
for (int j = 0; j < size2; j++) {
returnFloat[i, j] = array[i * size1 + j];
}
}
return returnFloat;
}
}
The line the error points to is:
this.array = new NativeArray<float>(size1 * size2, Allocator.TempJob);
Anyone know why this doesn't work? Thanks!
Answer by andrew-lukasik · Apr 25, 2021 at 06:16 PM
Key words are:
I am trying to create the following (job) struct inside a job
Mid-execution Jobs can allocate
Allocator.Temp
only. That's it. But before you even change that, read points 2 and 3.Scheduling a
Job
is possible from main application thread only and impossible from a Job.Jobs cannot access managed allocation such as
float[,]
, so this approach makes no sense either way.
Hmm, this struct is not it's own job sturct, just a struct that's being instantiated inside a job. Also, you can create float[,] inside a job, right? Just can't pass float[,] in and out of jobs. That's my understanding at least.
Thank you for the explanation for Allocator.Temp, I think I'll try changing this and see how it goes.
No, no managed memory access there mid-execution. Well... at least if you want to use Burst compiler in this job. You need to know that Burst is the single most useful thing about them, and also accessing NativeArray etc outside Jobs is super slow.you can create float[,] inside a job right?
Okay thanks for the info. So if I were to convert a NativeArray to an array using .ToArray() outside of the job, would that be considered slow? Is there a better way to achieve this?