ArchetypeChunk.Get_Count() crashes (Android only)

Hi !

I’m facing a strange issue that gives me a big headache.

I have this foreach on a system :

foreach (var (animatorRef, entity) in
                    SystemAPI.Query<MonsterAnimatorRef>()
                    .WithNone<Monster, Dying>()
                    .WithEntityAccess())
{
     animatorRef.Value.SetBool("Dying", true);
     ecb.AddComponent<Dying>(entity);
}

Monster and Dying are two Tag Components.
MonsterAnimatorRef is a CleanupComponent :

public class MonsterAnimatorRef : ICleanupComponentData
{
    public Animator Value;
}

Nothing crazy at first, it works well on Play Mode and on PC build.

On Android, when I kill a Monster (and therefore create an entity matching the query), the app crashes and logs this :

#00 Unity.Entities.ArchetypeChunk:get_Count () <.\Library\PackageCache\com.unity.entities@1.0.16\Unity.Entities\Iterators\ArchetypeChunkArray.cs:29>
#01 Unity.Entities.Internal.InternalEntityQueryEnumerator:MoveNextEntityRange (bool&,Unity.Entities.ArchetypeChunk&,int&,int&) <.\Library\PackageCache\com.unity.entities@1.0.16\Unity.Entities\Internal\InternalEntityQueryEnumerator.cs:166>
#02 MonsterDeathAnimateSystem/IFE_374884274_0/Enumerator:MoveNext () <C:\Users\Michael\jv\proto\arbalete\Unity.Entities.SourceGen.SystemGenerator\Unity.Entities.SourceGen.SystemGenerator.SystemGenerator\Temp\GeneratedCode\Assembly-CSharp\MonsterAnimateSystem__System_15642690750.g.cs:398>
#03 MonsterDeathAnimateSystem:OnUpdate (Unity.Entities.SystemState&) <C:\Users\Michael\jv\proto\arbalete\Assets\Game\Monsters\MonsterAnimateSystem.cs:50>
#04 MonsterDeathAnimateSystem:codegen__OnUpdate (intptr,intptr) <0x73>
#05 Unity.Entities.SystemBaseRegistry/<>c__DisplayClass9_0:b__0 (intptr,intptr) <.\Library\PackageCache\com.unity.entities@1.0.16\Unity.Entities\SystemBaseRegistry.cs:256>
#06 (wrapper native-to-managed) Unity.Entities.SystemBaseRegistry/<>c__DisplayClass9_0:b__0 (intptr,intptr)
#07 (wrapper managed-to-native) Unity.Entities.WorldUnmanagedImpl/Unity.Entities.UnmanagedUpdate_00001533$BurstDirectCall:wrapper_native_indirect_0xa3a0dfb8 (intptr&,void*)
#08 Unity.Entities.WorldUnmanagedImpl/Unity.Entities.UnmanagedUpdate_00001533$BurstDirectCall:Invoke (void*) <0x83>
#09 Unity.Entities.WorldUnmanagedImpl:UnmanagedUpdate (void*) <.\Library\PackageCache\com.unity.entities@1.0.16\Unity.Entities\WorldUnmanaged.cs:828>
#10 Unity.Entities.WorldUnmanagedImpl:UpdateSystem (Unity.Entities.SystemHandle) <.\Library\PackageCache\com.unity.entities@1.0.16\Unity.Entities\WorldUnmanaged.cs:894>
#11 Unity.Entities.ComponentSystemGroup:UpdateAllSystems () <.\Library\PackageCache\com.unity.entities@1.0.16\Unity.Entities\ComponentSystemGroup.cs:733>
#12 Unity.Entities.ComponentSystemGroup:OnUpdate () <.\Library\PackageCache\com.unity.entities@1.0.16\Unity.Entities\ComponentSystemGroup.cs:697>
#13 Unity.Entities.SystemBase:Update () <.\Library\PackageCache\com.unity.entities@1.0.16\Unity.Entities\SystemBase.cs:418>
#14 Unity.Entities.ScriptBehaviourUpdateOrder/DummyDelegateWrapper:TriggerUpdate () <.\Library\PackageCache\com.unity.entities@1.0.16\Unity.Entities\ScriptBehaviourUpdateOrder.cs:526>
#15 (wrapper runtime-invoke) object:runtime_invoke_void__this
(object,intptr,intptr,intptr)

Line #03 is refering to “MonsterAnimateSystem.cs:50” : it corresponds to the closing curly bracket of the suspicious foreach (line 8 on my copy/paste).
The program can not enter the foreach.
It crashes before, while iterating on Entities (see lines #00 and #01).

Do you have any idea why this is happening ? Or how I could debug this ?

If you need any additional information please ask me.

Thank you.

(Using Unity 2022.3.14f1 and Entities 1.0.16)

So, after few nights of struggle, I got it.

I’ll post what I understood in case it helps someone one day

To kill a Monster, I throw a Projectile at it.
The collision between the Projectile and the Monster is caught by an ITriggerEventsJob that :

  • Append a Damage to the Monster’s Damages DynamicBuffer
  • Destroy the projectile

It looks like, on Android, the TriggerEvent was fired multiple times before the destruction of the Projectile.
Causing unexpected behaviors (muliple damages applied before and after Monster death, multiple destroying of the Monster and the Projectile…).

That’s because the system calling the ITriggerEventsJob was updated in PhysicsSystemGroup group (after PhysicsSimulationGroup), and we souldn’t apply structural changes on entities involved on physics simulation:

Source : Physics data types | Unity Physics | 1.0.16

Destroying the projectile being a structural change, it cannot be done safely on PhysicsSystemGroup.

For now, I can simply remove the [UpdateInGroup(typeof(PhysicsSystemGroup))] and the TriggerEvents are fired only once, as expected.