Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question [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.     }