I am currently working my way into ECS and have written a job and am getting this error message:
ObjectDisposedException: The UNKNOWN_OBJECT_TYPE has been deallocated, it is not allowed to access it
I have now found out that it is this field that exists in my class:
private NativeList<Entity> _selectedEntityPlacementArea = new NativeList<Entity>();
My class looks like this, slightly shortened:
private NativeList<Entity> _selectedEntityPlacementArea = new NativeList<Entity>();
private bool UndergroundCheck(){
Job.WithCode(() => {
_selectedEntityPlacementArea.Add(checkEntity);
}).WithoutBurst().Run();
}
}```
What follows is the error mentioned above.
```public partial class BuildingPlacementSystem : SystemBase {
private NativeList<Entity> _selectedEntityPlacementArea = new NativeList<Entity>();
private bool UndergroundCheck(){
Job.WithCode(() => {
_selectedEntityPlacementArea.Add(checkEntity);
}).WithoutBurst().Run();
}
}```
Now I have tried it this way and get the same error:
```public partial class BuildingPlacementSystem : SystemBase {
private bool UndergroundCheck(){
NativeList<Entity> _selectedEntityPlacementArea = new NativeList<Entity>();
Job.WithCode(() => {
_selectedEntityPlacementArea.Add(checkEntity);
}).WithoutBurst().Run();
}
}```
Can someone explain to me how the error occurs and what I can do?
Many thanks!
The default constructor for a NativeList leaves it in an uninitialized and unallocated state. You just need to pass in an Allocator when constructing it or before using it.
If you want this list to stick around across many frames, use Allocator.Persistent. If you only need the list for one frame, you can use this.WorldUpdateAllocator instead.
private NativeList<Entity> _selectedEntityPlacementArea = new NativeList<Entity>(Allocator.Persistent);
But I still get the same error.
EDIT:
If I implement the NativList within the job again, it works. However, I need the data from the NativList later outside the list. Is there a way to have the data outside the job?
EDIT 2:
OK! Apparently easier than I thought. At the end of the job, the NativeList in the job is simply transferred to the NativeList outside the job.
While that link uses Allocator.TempJob, you can also use the WorldUpdateAllocator and pass collections allocated with that into jobs. You can read more about it here: World update allocator | Entities | 1.2.3