Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Question InvalidOperationException when performing raycast

Discussion in 'Physics for ECS' started by Micz84, Jul 3, 2020.

  1. Micz84

    Micz84

    Joined:
    Jul 21, 2012
    Posts:
    451
    I get this exception when I perform a raycast:

    InvalidOperationException: The Unity.Collections.NativeArray`1[Unity.Physics.BoundingVolumeHierarchy+Node] has been deallocated, it is not allowed to access it
    Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle.CheckReadAndThrowNoEarlyOut (Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle handle) (at <c85ab98a3257443eaa6aaaea77733582>:0)
    Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle.CheckReadAndThrow (Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle handle) (at <c85ab98a3257443eaa6aaaea77733582>:0)
    Unity.Collections.LowLevel.Unsafe.NativeArrayUnsafeUtility.GetUnsafeReadOnlyPtr[T] (Unity.Collections.NativeArray`1[T] nativeArray) (at <c85ab98a3257443eaa6aaaea77733582>:0)
    Unity.Physics.BoundingVolumeHierarchy..ctor (Unity.Collections.NativeArray`1[T] nodes, Unity.Collections.NativeArray`1[T] nodeFilters) (at Library/PackageCache/com.unity.physics@0.4.0-preview.5/Unity.Physics/Collision/Geometry/BoundingVolumeHierarchy.cs:26)
    Unity.Physics.Broadphase+Tree.get_BoundingVolumeHierarchy () (at Library/PackageCache/com.unity.physics@0.4.0-preview.5/Unity.Physics/Collision/World/Broadphase.cs:363)
    Unity.Physics.Broadphase.CastRay[T] (Unity.Physics.RaycastInput input, Unity.Collections.NativeArray`1[T] rigidBodies, T& collector) (at Library/PackageCache/com.unity.physics@0.4.0-preview.5/Unity.Physics/Collision/World/Broadphase.cs:513)
    Unity.Physics.CollisionWorld.CastRay[T] (Unity.Physics.RaycastInput input, T& collector) (at Library/PackageCache/com.unity.physics@0.4.0-preview.5/Unity.Physics/Collision/World/CollisionWorld.cs:199)
    Unity.Physics.PhysicsWorld.CastRay[T] (Unity.Physics.RaycastInput input, T& collector) (at Library/PackageCache/com.unity.physics@0.4.0-preview.5/Unity.Physics/Dynamics/World/PhysicsWorld.cs:74)
    Unity.Physics.QueryWrappers.RayCast[T] (T& target, Unity.Physics.RaycastInput input) (at Library/PackageCache/com.unity.physics@0.4.0-preview.5/Unity.Physics/Collision/Queries/Collidable.cs:99)
    Unity.Physics.PhysicsWorld.CastRay (Unity.Physics.RaycastInput input) (at Library/PackageCache/com.unity.physics@0.4.0-preview.5/Unity.Physics/Dynamics/World/PhysicsWorld.cs:69)
    Systems.EnemySystem+MovementRayCast.CheckRay (Unity.Mathematics.float3 pos, Unity.Mathematics.float3 dir, Unity.Mathematics.float3 currentDirection) (at Assets/Scripts/Systems/EnemySystem.cs:101)
    Systems.EnemySystem+<>c__DisplayClass_OnUpdate_LambdaJob0.OriginalLambdaBody (Movable& movable, Components.Enemy& enemy, Unity.Transforms.Translation& translation) (at Assets/Scripts/Systems/EnemySystem.cs:43)
    Systems.EnemySystem+<>c__DisplayClass_OnUpdate_LambdaJob0.IterateEntities (Unity.Entities.ArchetypeChunk& chunk, Systems.EnemySystem+<>c__DisplayClass_OnUpdate_LambdaJob0+LambdaParameterValueProviders+Runtimes& runtimes) (at <de25ec77f2f14dcbab34021a6ebda110>:0)
    Systems.EnemySystem+<>c__DisplayClass_OnUpdate_LambdaJob0.Execute (Unity.Entities.ArchetypeChunk chunk, System.Int32 chunkIndex, System.Int32 firstEntityIndex) (at <de25ec77f2f14dcbab34021a6ebda110>:0)
    Unity.Entities.JobChunkExtensions.RunWithoutJobs[T] (T& jobData, Unity.Entities.ArchetypeChunkIterator& chunkIterator) (at Library/PackageCache/com.unity.entities@0.11.1-preview.4/Unity.Entities/IJobChunk.cs:157)
    Systems.EnemySystem+<>c__DisplayClass_OnUpdate_LambdaJob0.RunWithoutJobSystem (Unity.Entities.ArchetypeChunkIterator* archetypeChunkIterator, System.Void* jobData) (at <de25ec77f2f14dcbab34021a6ebda110>:0)
    Unity.Entities.InternalCompilerInterface.RunJobChunk[T] (T& jobData, Unity.Entities.EntityQuery query, Unity.Entities.InternalCompilerInterface+JobChunkRunWithoutJobSystemDelegate functionPointer) (at Library/PackageCache/com.unity.entities@0.11.1-preview.4/Unity.Entities/CodeGeneratedJobForEach/LambdaJobDescription.cs:375)
    Systems.EnemySystem.OnUpdate () (at Assets/Scripts/Systems/EnemySystem.cs:36)
    Unity.Entities.SystemBase.Update () (at Library/Pa

    Do I need to add some dependency to my job?

    I have made a simplified version:

    Code (CSharp):
    1.  
    2. public class EnemySystem : SystemBase
    3. {
    4.         private PhysicsWorld _PhysicsWorld;
    5.  
    6.         private Random random = new Random(22);
    7.  
    8.         protected override void OnCreate()
    9.         {
    10.             base.OnCreate();
    11.             _PhysicsWorld = World.DefaultGameObjectInjectionWorld.GetExistingSystem<BuildPhysicsWorld>().PhysicsWorld;
    12.         }
    13.  
    14.         protected override void OnUpdate()
    15.         {
    16.             var rayCaster = new MovementRayCast
    17.             {
    18.                 PhysicsWorld = _PhysicsWorld
    19.             };
    20.             random.NextInt();
    21.             var rand = random;
    22.             var frame = UnityEngine.Time.frameCount;
    23.             Entities.ForEach((ref Movable movable, ref Enemy enemy, in Translation translation) =>
    24.             {
    25.                 var ray = new RaycastInput
    26.                 {
    27.                     Start = translation.Value,
    28.                     End = translation.Value + movable.direction * 0.9f,
    29.                     Filter = CollisionFilter.Default
    30.                     /*new CollisionFilter
    31.                     {
    32.                         GroupIndex = 0,
    33.                         BelongsTo = 1u << 1,
    34.                         CollidesWith = 1u << 2
    35.                     }*/
    36.                 };
    37.                 Debug.Log($"WallAhead {rayCaster.PhysicsWorld.CastRay(ray)}");
    38.            }).WithoutBurst().Run();
    39.         }
    40.  
    41.     private struct MovementRayCast
    42.     {
    43.         [ReadOnly] public PhysicsWorld PhysicsWorld;
    44.     }
    45.  }
    46.  
     
  2. Micz84

    Micz84

    Joined:
    Jul 21, 2012
    Posts:
    451
    I have found an issue can be closed.
     
    petarmHavok likes this.