Search Unity

Unloading Subscene creates blob asset exception

Discussion in 'Physics for ECS' started by Occuros, Jan 19, 2021.

  1. Occuros

    Occuros

    Joined:
    Sep 4, 2018
    Posts:
    300
    When unloading subscenes a `The BlobAssetReference is not valid` is thrown every frame from the physics system.
    (until the subscene is loaded again).


    I unloaded the subscene in the following way:

    Code (CSharp):
    1.  
    2.  Entities
    3.   .WithoutBurst()
    4.   .WithStructuralChanges()
    5.   .ForEach((Entity entity, SubScene scene) =>
    6.   {                        
    7.     if (isDead)
    8.     {
    9.         Debug.Log("Player Died, Restarting");
    10.         sceneSystem.UnloadScene(scene.SceneGUID);
    11.      }
    12.    }
    13. ).Run();
    14.  

    Is the way the subscenes are unloaded incorrect, or does the physics system need to be notified about the unloading of the scene prior to unloading it?

    Code (CSharp):
    1. InvalidOperationException: The BlobAssetReference is not valid. Likely it has already been unloaded or released.
    2. Unity.Entities.BlobAssetReferenceData.ValidateNonBurst () (at Library/PackageCache/com.unity.entities@0.16.0-preview.21/Unity.Entities/Blobs.cs:253)
    3. Unity.Entities.BlobAssetReferenceData.ValidateAllowNull () (at Library/PackageCache/com.unity.entities@0.16.0-preview.21/Unity.Entities/Blobs.cs:280)
    4. Unity.Entities.BlobAssetReference`1[T].GetUnsafePtr () (at Library/PackageCache/com.unity.entities@0.16.0-preview.21/Unity.Entities/Blobs.cs:332)
    5. Unity.Physics.ManifoldQueries.BodyBody (Unity.Physics.RigidBody& rigidBodyA, Unity.Physics.RigidBody& rigidBodyB, Unity.Physics.MotionVelocity& motionVelocityA, Unity.Physics.MotionVelocity& motionVelocityB, System.Single collisionTolerance, System.Single timeStep, Unity.Physics.BodyIndexPair pair, Unity.Collections.NativeStream+Writer& contactWriter) (at Library/PackageCache/com.unity.physics@0.5.1-preview.2/Unity.Physics/Collision/Queries/Manifold.cs:48)
    6. Unity.Physics.NarrowPhase+ParallelCreateContactsJob.ExecuteImpl (Unity.Physics.PhysicsWorld& world, System.Single timeStep, Unity.Collections.NativeArray`1[T] dispatchPairs, System.Int32 dispatchPairReadOffset, System.Int32 numPairsToRead, Unity.Collections.NativeStream+Writer& contactWriter) (at Library/PackageCache/com.unity.physics@0.5.1-preview.2/Unity.Physics/Dynamics/Simulation/Narrowphase.cs:111)
    7. Unity.Physics.NarrowPhase+ParallelCreateContactsJob.Execute (System.Int32 workItemIndex) (at Library/PackageCache/com.unity.physics@0.5.1-preview.2/Unity.Physics/Dynamics/Simulation/Narrowphase.cs:78)
    8. Unity.Jobs.IJobParallelForDeferExtensions+JobParallelForDeferProducer`1[T].Execute (T& jobData, System.IntPtr additionalPtr, System.IntPtr bufferRangePatchData, Unity.Jobs.LowLevel.Unsafe.JobRanges& ranges, System.Int32 jobIndex) (at Library/PackageCache/com.unity.jobs@0.7.0-preview.17/Unity.Jobs/IJobParallelForDefer.cs:62)
     
  2. milos85miki

    milos85miki

    Joined:
    Nov 29, 2019
    Posts:
    197
    When do you run the scene unload code?
     
  3. Occuros

    Occuros

    Joined:
    Sep 4, 2018
    Posts:
    300
    I tried to run it in both the
    Code (CSharp):
    1. InitializationSystemGroup
    and the normal
    Code (CSharp):
    1. SimulationSystemGroup
    .
     
  4. milos85miki

    milos85miki

    Joined:
    Nov 29, 2019
    Posts:
    197
    Thanks for the info!

    The exception suggests that some entities were destroyed/changed during the physics step. However, I'm not sure why that would happen if you unload the scene in a system under InitializationSystemGroup.

    Here are some things you could try to narrow the investigation:
    - try to run your system in FixedStepSimulationSystemGroup, with UpdateBefore(BuildPhysicsWorld)
    - remove the PhysicsStep component to disable physics completely and see if other things work
    - remove PhysicsBody component from all objects in the subscene that you're trying to remove, to see if those entities are causing the exception

    It'd be great if you could share the minimal project that reproduces this problem, so I could debug through if the above tips don't help.
     
  5. elJoel

    elJoel

    Joined:
    Sep 7, 2016
    Posts:
    125
    Have you tried this?
    Code (CSharp):
    1.     [UpdateInGroup(typeof(SceneSystemGroup))]
    2.     [UpdateBefore(typeof(SceneSystem))]
    3.  
     
  6. Occuros

    Occuros

    Joined:
    Sep 4, 2018
    Posts:
    300
  7. milos85miki

    milos85miki

    Joined:
    Nov 29, 2019
    Posts:
    197
    Hi @Occuros ,

    I think I've found the source of the problem you're hitting - it's a bug that we already fixed, but it'll be available with the next release (ETA later this month). The system in Unity.Physics\ECS\Base\Systems\BuildPhysicsWorld.cs needs to have a AlwaysUpdateSystem attribute.

    If you'd like to give it a try before the new release, you can make Unity.Physics a local package (see this thread for guidelines) and just add the attribute.

    Hoping this helps!
     
    Occuros likes this.
  8. Occuros

    Occuros

    Joined:
    Sep 4, 2018
    Posts:
    300