Hi All,
So I have what I think it’s a very simple setup trying to instantiate prefabs, where I have one authoring object holding a prefab, then I instantiate that prefab X number of times once I click play.
I don’t use any NativeArrays but I keep getting “A Native Collection has not been disposed, resulting in a memory leak”.
No idea what would be the cause of the problem.
Below is my setup:
This is my MonoBehaviour with a converter, which maps it to a IComponentData (it has a prefab in it):
public class Knight : MonoBehaviour, IConvertGameObjectToEntity
{
public GameObject Prefab;
public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
{
var prefab = conversionSystem.GetPrimaryEntity(Prefab);
var component = new KnightPrefabComponent { prefabEntity = prefab };
dstManager.AddComponentData(entity, component);
}
}
Then here is the component definition:
public struct KnightPrefabComponent : IComponentData
{
public Entity prefabEntity;
}
And here I tell ECS that I want prefabs:
[UpdateInGroup(typeof(GameObjectDeclareReferencedObjectsGroup))]
class PrefabConverterDeclare : GameObjectConversionSystem
{
protected override void OnUpdate()
{
Entities.ForEach((Knight prefabReference) =>
{
DeclareReferencedPrefab(prefabReference.Prefab);
});
}
}
This is the code that spawns the entities:
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
using UnityEngine;
using Random = Unity.Mathematics.Random;
public class EntitySpawnerSystem : ComponentSystem
{
private float spawnTimer;
private Random random;
private int maxInstances = 10;
private int instances;
protected override void OnCreate()
{
random = new Random(56);
instances = 0;
}
protected override void OnUpdate()
{
//spawnTimer -= Time.DeltaTime;
if (instances < maxInstances)
{
instances++;
//spawnTimer = .01f;
Debug.Log($"Instance: {instances}");
Entities.ForEach((ref KnightPrefabComponent component) =>
{
var entity = EntityManager.Instantiate(component.prefabEntity);
EntityManager.SetComponentData(entity, new Translation()
{
Value = new float3(random.NextFloat(0, 200), 0, random.NextFloat(0, 200))
});
});
}
}
}
Stacktrace (none of my classes shows up!):
A Native Collection has not been disposed, resulting in a memory leak. Allocated from:
Unity.Collections.NativeArray`1:.ctor(Int32, Allocator, NativeArrayOptions)
Unity.Scenes.SceneSectionStreamingSystem:ExtractEntityRemapRefs(EntityManager, NativeArray`1&) (at Library\PackageCache\com.unity.entities@0.17.0-preview.42\Unity.Scenes\SceneSectionStreamingSystem.cs:257)
Unity.Scenes.SceneSectionStreamingSystem:MoveEntities(EntityManager, Entity) (at Library\PackageCache\com.unity.entities@0.17.0-preview.42\Unity.Scenes\SceneSectionStreamingSystem.cs:198)
Unity.Scenes.SceneSectionStreamingSystem:UpdateLoadOperation(AsyncLoadSceneOperation, World, Entity) (at Library\PackageCache\com.unity.entities@0.17.0-preview.42\Unity.Scenes\SceneSectionStreamingSystem.cs:371)
Unity.Scenes.SceneSectionStreamingSystem:ProcessActiveStreams() (at Library\PackageCache\com.unity.entities@0.17.0-preview.42\Unity.Scenes\SceneSectionStreamingSystem.cs:314)
Unity.Scenes.SceneSectionStreamingSystem:OnUpdate() (at Library\PackageCache\com.unity.entities@0.17.0-preview.42\Unity.Scenes\SceneSectionStreamingSystem.cs:531)
Unity.Entities.ComponentSystem:Update() (at Library\PackageCache\com.unity.entities@0.17.0-preview.42\Unity.Entities\ComponentSystem.cs:114)
Unity.Entities.ComponentSystemGroup:UpdateAllSystems() (at Library\PackageCache\com.unity.entities@0.17.0-preview.42\Unity.Entities\ComponentSystemGroup.cs:472)
Unity.Entities.ComponentSystemGroup:OnUpdate() (at Library\PackageCache\com.unity.entities@0.17.0-preview.42\Unity.Entities\ComponentSystemGroup.cs:417)
Unity.Entities.ComponentSystem:Update() (at Library\PackageCache\com.unity.entities@0.17.0-preview.42\Unity.Entities\ComponentSystem.cs:114)
Unity.Entities.ComponentSystemGroup:UpdateAllSystems() (at Library\PackageCache\com.unity.entities@0.17.0-preview.42\Unity.Entities\ComponentSystemGroup.cs:472)
Unity.Entities.ComponentSystemGroup:OnUpdate() (at Library\PackageCache\com.unity.entities@0.17.0-preview.42\Unity.Entities\ComponentSystemGroup.cs:417)
Unity.Entities.ComponentSystem:Update() (at Library\PackageCache\com.unity.entities@0.17.0-preview.42\Unity.Entities\ComponentSystem.cs:114)
Unity.Entities.DummyDelegateWrapper:TriggerUpdate() (at Library\PackageCache\com.unity.entities@0.17.0-preview.42\Unity.Entities\ScriptBehaviourUpdateOrder.cs:333)
