- Home /
 
Problem is not reproducible
Entities.Foreach.Run causes Invalid IL code
In my system, it is necessary to go through some entities if a certain condition is met. To do this, I use Entities.Foreach.Run. As a result, an error occurs:
 InvalidProgramException: Invalid IL code in ECS.Systems.ContainerSelectSystem:OnUpdate (): IL_01a7:
 
               However, if you call Schedule instead of Run, there will be no error. Also, there will be no error if I make exactly the same bypass from the If block.
Example code:
     [UpdateInGroup(typeof(SimulationSystemGroup))]
     //[DisableAutoCreation]
     public class ContainerSelectSystem : SystemBase
     {
         public const float maxDistance = 300f;
 
         BuildPhysicsWorld _buildPhysicsWorld;
 
         protected override void OnCreate()
         {
             _buildPhysicsWorld = World.GetExistingSystem<BuildPhysicsWorld>();
             RequireForUpdate(GetEntityQuery(new EntityQueryDesc
             {
                 All = new ComponentType[] { typeof(Container) }
             }));
         }
 
         protected override void OnUpdate()
         {
             if (Input.GetMouseButtonDown(0) && (Camera.main != null))
             {
                 var mousePosition = Input.mousePosition;
                 var ray = Camera.main.ScreenPointToRay(mousePosition);
                 var raycastInput = new RaycastInput
                 {
                     Start = ray.origin,
                     End = ray.origin + ray.direction * maxDistance,
                     Filter = CollisionFilter.Default,
                 };
                 
                 var collisionWorld = _buildPhysicsWorld.PhysicsWorld.CollisionWorld;
 
                 RaycastHit hit;
                 if (collisionWorld.CastRay(raycastInput, out hit))
                 {
 
                     var index = EntityManager.GetComponentData<Container>(hit.Entity).Index;
                     Debug.Log("Сheck: " + index);
 
                     var stopwatch = new Stopwatch();
                     stopwatch.Start();
 
                     Entities
                         .WithoutBurst()
                         .ForEach((Entity entity, in Container container) =>
                         {
                             if (container.Index.y == index.y)
                                 Debug.Log(container.Index);
                         })
                         .Run();
 
                     stopwatch.Stop();
                     Debug.Log(stopwatch.Elapsed.TotalMilliseconds);
                 }
             }
         }
     }
 
               Error message you provided is incomplete. Copy & paste full message details.
I have no idea which version of unity.entities and unity.physics packages you're running here. But I tested this code on both 0.17p42+0.6.0p3 and 0.50+0.50 and I see no errors. 
I found the reason quite by accident. The error was thrown because of StopWatch. The solution was also found by chance: instead of creating a new StopWatch variable before traversing entities, I put it in the class field. I don't understand how it works behind the scenes.