I am learning the usage of ECS by creating a mini game. While it has been going well, I have come across an issue that I am uncertain on how to fix.
The game generates a random amount of monsters and I have turrets that shoot these monsters when detected based on range. To search if monsters are within the range I want to create an EntityQuery of all entities with Tag_Monster and then loop it in ForEach, while using job and burst. However I am getting the error
A Native Collection has not been disposed, resulting in a memory leak. Allocated from:
Unity.Collections.NativeArray`1:.ctor(Int32, Allocator, NativeArrayOptions)
Unity.Entities.ChunkIterationUtility:CreateEntityArray(UnsafeMatchingArchetypePtrList, Allocator, ArchetypeChunkEntityType, EntityQuery, EntityQueryFilter&, JobHandle&, JobHandle) (at Library\PackageCache\com.unity.entities@0.11.1-preview.4\Unity.Entities\Iterators\ChunkIterationUtility.cs:151)
Unity.Entities.EntityQueryImpl:ToEntityArray(Allocator, EntityQuery) (at Library\PackageCache\com.unity.entities@0.11.1-preview.4\Unity.Entities\Iterators\EntityQuery.cs:477)
Unity.Entities.EntityQuery:ToEntityArray(Allocator) (at Library\PackageCache\com.unity.entities@0.11.1-preview.4\Unity.Entities\Iterators\EntityQuery.cs:1217)
TargetClosestMonsterSystem:OnUpdate() (at Assets\Script\Systems\TargetClosestMonsterSystem.cs:15)
Unity.Entities.SystemBase:Update() (at Library\PackageCache\com.unity.entities@0.11.1-preview.4\Unity.Entities\SystemBase.cs:398)
Unity.Entities.ComponentSystemGroup:UpdateAllSystems() (at Library\PackageCache\com.unity.entities@0.11.1-preview.4\Unity.Entities\ComponentSystemGroup.cs:445)
Unity.Entities.ComponentSystemGroup:OnUpdate() (at Library\PackageCache\com.unity.entities@0.11.1-preview.4\Unity.Entities\ComponentSystemGroup.cs:398)
Unity.Entities.ComponentSystem:Update() (at Library\PackageCache\com.unity.entities@0.11.1-preview.4\Unity.Entities\ComponentSystem.cs:109)
Unity.Entities.DummyDelegateWrapper:TriggerUpdate() (at Library\PackageCache\com.unity.entities@0.11.1-preview.4\Unity.Entities\ScriptBehaviourUpdateOrder.cs:192)
protected override void OnUpdate()
{
EntityQuery monsterQuery = GetEntityQuery(ComponentType.ReadOnly<Tag_Monster>());
NativeArray<Entity> monsters = monsterQuery.ToEntityArray(Allocator.TempJob);
Entities.WithAll<Tag_Turret>().WithNone<TurretTarget>().ForEach((Entity turret, in Translation turretTranslation, in TurretStats turretStats) => {
}).ScheduleParallel();
}
I researched on the subject to try and find a way to dispose of EntityQuery and NativeArray, however none of the methods resolved it for me.
Thanks for all the assistance.