I get a crash that seems to be dependent on the way that the code is run in the Editor.
When I click Play the first time my game runs just fine. When I stop the game and run it a second time it crashes when I try to call CreateEntity on EntityCommanBuffer:
NullReferenceException: Object reference not set to an instance of an object
Unity.Entities.ComponentChunkIterator.UpdateCacheToCurrentChunk (Unity.Entities.ComponentChunkCache& cache, System.Boolean isWriting, System.Int32 indexInComponentGroup) (at Library/PackageCache/com.unity.entities@0.0.12-preview.19/Unity.Entities/Iterators/ComponentChunkIterator.cs:368)
Unity.Entities.ComponentChunkIterator.MoveToEntityIndexAndUpdateCache (System.Int32 index, Unity.Entities.ComponentChunkCache& cache, System.Boolean isWriting) (at Library/PackageCache/com.unity.entities@0.0.12-preview.19/Unity.Entities/Iterators/ComponentChunkIterator.cs:387)
Unity.Entities.EntityArray.get_Item (System.Int32 index) (at Library/PackageCache/com.unity.entities@0.0.12-preview.19/Unity.Entities/Iterators/EntityArray.cs:46)
ECS.Systems.Board.BoardAttachSystem.OnUpdate () (at Assets/Scripts/ECS/Systems/Board/BoardAttachSystem.cs:30)
Unity.Entities.ComponentSystem.InternalUpdate () (at Library/PackageCache/com.unity.entities@0.0.12-preview.19/Unity.Entities/ComponentSystem.cs:374)
Unity.Entities.ScriptBehaviourManager.Update () (at Library/PackageCache/com.unity.entities@0.0.12-preview.19/Unity.Entities/ScriptBehaviourManager.cs:77)
Unity.Entities.ScriptBehaviourUpdateOrder+DummyDelagateWrapper.TriggerUpdate () (at Library/PackageCache/com.unity.entities@0.0.12-preview.19/Unity.Entities/ScriptBehaviourUpdateOrder.cs:703)
This will happen over and over again until i restart Unity. Changing the scenes doesn’t help
I ran into this issue too, although in a totally different use case from an EntityCommandBuffer.
This code breaks after the first run:
public Entity GetFirstEntity(ComponentGroup group)
{
var entities = group.GetEntityArray();
if (entities.Length <= 0)
{
throw new InvalidOperationException($"Failed to find an entity in {group}");
}
return entities[0];
}
This alternative code works pretty consistently:
public Entity GetFirstEntity(ComponentGroup group)
{
if (group.CalculateLength() <= 0)
{
throw new InvalidOperationException($"Failed to find an entity in {group}");
}
var entityType = GetArchetypeChunkEntityType();
var chunks = group.CreateArchetypeChunkArray(Allocator.Temp);
var entities = chunks[0].GetNativeArray(entityType);
var entity = entities[0];
chunks.Dispose();
return entity;
}
EntityArray seems to be the culprit. Note that I am using a custom world and manually creating and updating systems.
@DreamingImLatios Uh, yes, I forgot to add this. I was using ComponentGroups. Today I moved all of my systems to using [Inject] and everything works fine.
@psuong That’s why I was using ComponentGroups in the first place. Once we get a properly working alternative I’ll move from [Inject]. Translating between [Inject] method and ComponentGroups is pretty straightforward
@eizenhorn See the code example in the second comment ( by @DreamingImLatios ). The exact same thing is happening for me. Whenever I use ComponentGroup.GetEntityArray() I get crashes on EntityCommandBuffer.CreateEntity() in a subsequent launches in GameMode