Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved [Unity.Physics.BoundingVolumeHierarchy+Node] has been deallocated, it is not allowed to access it

Discussion in 'Physics for ECS' started by iamshenkui_gee, Jul 9, 2022.

  1. iamshenkui_gee

    iamshenkui_gee

    Joined:
    Apr 24, 2021
    Posts:
    30
    I am finishing the dashing system in my game. I got the suggestion about using the ray cast function in physics. However, I met the following problem and I am not sure how I can solve it.

    ObjectDisposedException: 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 <f1212ad1dec44ce7b7147976b91869c3>:0)
    Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle.CheckReadAndThrow (Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle handle) (at <f1212ad1dec44ce7b7147976b91869c3>:0)
    Unity.Collections.LowLevel.Unsafe.NativeArrayUnsafeUtility.GetUnsafeReadOnlyPtr[T] (Unity.Collections.NativeArray`1[T] nativeArray) (at <f1212ad1dec44ce7b7147976b91869c3>:0)
    Unity.Physics.BoundingVolumeHierarchy..ctor (Unity.Collections.NativeArray`1[T] nodes, Unity.Collections.NativeArray`1[T] nodeFilters)



    I will also list my code below; can anyone tell me what the problem is?
    Code (CSharp):
    1. public partial class PlayerDashSystem : SystemBase
    2. {
    3.  
    4.     float dashDistance = 100f;
    5.     BuildPhysicsWorld buildPhysicsWorld;
    6.     CollisionWorld collisionWorld;
    7.     UnityEngine.Ray ray;
    8.  
    9.     EntityQuery m_playerEntityQuery;
    10.  
    11.     protected override void OnCreate()
    12.     {
    13.         buildPhysicsWorld = World.GetOrCreateSystem<BuildPhysicsWorld>();
    14.         collisionWorld = buildPhysicsWorld.PhysicsWorld.CollisionWorld;
    15.         m_playerEntityQuery = GetEntityQuery(ComponentType.ReadWrite<PlayerTag>());
    16.  
    17.     }
    18.     protected override void OnUpdate()
    19.     {
    20.         if (m_playerEntityQuery.CalculateEntityCountWithoutFiltering() < 1) return;
    21.  
    22.         if (Input.GetMouseButton(0))
    23.         {
    24.             ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    25.             var rayStart = new Vector3(ray.origin.x, ray.origin.y, 0);
    26.             ray.direction = new Vector3(ray.direction.x, ray.direction.y,0);
    27.             var rayEnd = ray.GetPoint(dashDistance);
    28.  
    29.             Debug.Log(Raycast(rayStart, rayEnd));
    30.         }
    31.  
    32.     }
    33.  
    34.     private Entity Raycast(float3 initialPosition, float3 targetPosition)
    35.     {
    36.         RaycastInput raycastInput = new RaycastInput
    37.         {
    38.             Start = initialPosition,
    39.             End = targetPosition,
    40.             Filter = new CollisionFilter
    41.             {
    42.                 BelongsTo = 0xffffffff,
    43.                 CollidesWith = 0xffffffff
    44.             }
    45.         };
    46.  
    47.         Unity.Physics.RaycastHit raycastHit = new Unity.Physics.RaycastHit();
    48.  
    49.         if(collisionWorld.CastRay(raycastInput, out raycastHit))
    50.         {
    51.             Entity hitEntity = buildPhysicsWorld.PhysicsWorld.Bodies[raycastHit.RigidBodyIndex].Entity;
    52.             return hitEntity;
    53.         }
    54.         else
    55.         {
    56.             return Entity.Null;
    57.         }
    58.     }
     
  2. gabrieloc

    gabrieloc

    Joined:
    Apr 6, 2014
    Posts:
    48
    did you figure out a solution for this?
     
  3. daniel-holz

    daniel-holz

    Unity Technologies

    Joined:
    Sep 17, 2021
    Posts:
    277
    The way the CollisionWorld is obtained is incorrect in the code snippet provided above.
    It has to be obtained every frame since its content is rebuild and its internal native containers potentially reallocated.

    Here is an example of how to obtain it (through the PhysicsWorld in the PhysicsWorldSingleton component).

    Also have a look at the PhysicsSamples project which contains various raycasting examples, such as "7. Query / Cast, Collider casting and ray casting".