ITriggerEventsJob crash after clearing all entities and recreating the world

Hey everyone,

so I have a Game scene with world with a bunch of entities doing a bunch of physics interaction. Everything works fine. When my game logic is done, I clear everything with:

        public void OnQuitClicked() { // ButtonCallback
            var entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
            entityManager.DestroyEntity(entityManager.UniversalQuery);

            sceneManager.LoadScene("Menu"); // Async
        }

When I relaunch the exact same Game scene, the very first frame (I assume) where a trigger is alive, I have get the following exception:

IndexOutOfRangeException: Index 144 is out of range of '6' Length.
Unity.Collections.NativeSlice`1[T].FailOutOfRangeError (System.Int32 index) (at /Users/builduser/buildslave/unity/build/Runtime/Export/NativeArray/NativeSlice.cs:222)
Unity.Collections.NativeSlice`1[T].CheckReadIndex (System.Int32 index) (at /Users/builduser/buildslave/unity/build/Runtime/Export/NativeArray/NativeSlice.cs:174)
Unity.Collections.NativeSlice`1[T].get_Item (System.Int32 index) (at /Users/builduser/buildslave/unity/build/Runtime/Export/NativeArray/NativeSlice.cs:199)
Unity.Physics.ITriggerEventJobExtensions+TriggerEventJobProcess`1[T].Execute (Unity.Physics.ITriggerEventJobExtensions+TriggerEventJobData`1[T]& jobData, System.IntPtr additionalData, System.IntPtr bufferRangePatchData, Unity.Jobs.LowLevel.Unsafe.JobRanges& ranges, System.Int32 jobIndex) (at Library/PackageCache/com.unity.physics@0.2.5-preview.1/Unity.Physics/Dynamics/Simulation/ITriggerEventsJob.cs:104)

It’s almost like a trigger event was lingering around from before when I nuked all Entities :face_with_spiral_eyes:
Has anyone experienced this or found a workaround?

Note: My TriggerEventsJob is a [BurstCompile] struct MyTriggerJob : ITriggerEventsJob, but since my class isn’t even in the stacktrace, it shouldn’t matter I guess.

One possible source of the problem is that maybe my entityManager.DestroyEntity(entityManager.UniversalQuery); runs at a somewhat inopportune time (given that it’s from a button click, it should be on the main thread, right?) and the PhysicsWorld gets confused and then barfs up this exception once triggers are happening again?

Thanks in advance :slight_smile:

(X-post for wider audience: https://forum.unity.com/threads/itriggereventsjob-crash-after-clearing-all-entities-and-recreating-the-world.837586/ )

At what point is exactly your MyTriggerJob scheduled? If it somehow runs before the physics step ends and you’ve just disposed the whole world, it might be trying to access uninitialized stream of trigger events.

1 Like

The MyTriggerJob : ITriggerEventsJob physics job runs in the SimulationSystemGroup.

Is the assumption wrong that UI interactions, like button click, run after the DOTS systems? Or do they just run asap on the main thread, possible in the middle of the DOTS systems? :eyes:

One thing I could try is instead of destroying all entities in the click handler, I could set a flag in a EndOfGameSystem as part of the LateSimulationSystemGroup :face_with_spiral_eyes:

I’m not sure to be honest about when exactly the UI interactions happen, but I know I’ve seen problems trying to have correct order between DOTS and non-DOTS code paths, like the one you are having.

I suggest doing the trick with a flag and let’s see what happens. Also make sure your job runs after StepPhysicsWorld is finished, otherwise you’ll get inconsistent behavior for sure.