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. Dismiss Notice

Question why WithEntityAccess throw error

Discussion in 'Entity Component System' started by qcbf1, Aug 8, 2023.

  1. qcbf1

    qcbf1

    Joined:
    Nov 1, 2013
    Posts:
    19
    thank everybody, thank help me!
    i don't know, why call "AddComponent", then ".WithEntityAccess()" throw exception.
    entities package version 1.0.11
    unity version 2022.3.6
    ------------------------------------------------
    two system, this is my sources code.

    Jump System, here handle JumpComponent, add force and remove JumpComponent
    Code (CSharp):
    1.  
    2. using var cmd = new EntityCommandBuffer(Allocator.Temp);
    3.  
    4. /*use this code it ok.*/ //foreach (var (Jump, Velocity) in SystemAPI.Query<RefRO<JumpCmpt>, RefRW<PhysicsVelocity>>()) // no error
    5.  
    6. /*here throw error*/ foreach (var (Jump, Velocity, Entity) in SystemAPI.Query<RefRO<JumpCmpt>, RefRW<PhysicsVelocity>>().WithEntityAccess()) // throw error
    7. {
    8.     Velocity.ValueRW.Linear += Jump.ValueRO.Force;
    9.     cmd.RemoveComponent<JumpCmpt>(Entity);
    10. }
    11. cmd.Playback(state.EntityManager);
    12.  

    Input Handle System, receive input then here add JumComponent
    Code (CSharp):
    1.          
    2. if (Input.GetMouseButtonDown(0))
    3. {
    4.     using var cmd = new EntityCommandBuffer(Allocator.Temp);
    5.     foreach (var (Debug, Entity) in SystemAPI.Query<RefRO<DebugCmpt>>().WithEntityAccess())
    6.     {
    7.         if (Debug.ValueRO.Name == "Player")
    8.         {
    9.             cmd.AddComponent(Entity, new JumpCmpt
    10.             {
    11.                 Force = new float3(0, 10, 0)
    12.             });
    13.             break;
    14.         }
    15.     }
    16.     cmd.Playback(state.EntityManager);
    17. }
    18.  

    -----------------------------------------------------------------
    this is my throw call stack
    Code (CSharp):
    1.  
    2. ObjectDisposedException: Attempted to access Unity.Entities.EntityTypeHandle which has been invalidated by a structural change.
    3. Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle.CheckReadAndThrowNoEarlyOut (Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle handle) (at <b41119cc6741409ea29f63c7f98de938>:0)
    4. Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle.CheckReadAndThrow (Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle handle) (at <b41119cc6741409ea29f63c7f98de938>:0)
    5. Unity.Entities.ArchetypeChunk.GetEntityDataPtrRO (Unity.Entities.EntityTypeHandle entityTypeHandle) (at ./Library/PackageCache/com.unity.entities@1.0.11/Unity.Entities/Iterators/ArchetypeChunkArray.cs:1531)
    6. Unity.Entities.Internal.InternalCompilerInterface.UnsafeGetChunkEntityArrayIntPtr (Unity.Entities.ArchetypeChunk chunk, Unity.Entities.EntityTypeHandle typeHandle) (at ./Library/PackageCache/com.unity.entities@1.0.11/Unity.Entities/Internal/InternalCompilerInterface.cs:228)
    7. Game.JumpSys+IFE_384976868_0+TypeHandle.Resolve (Unity.Entities.ArchetypeChunk archetypeChunk) (at Unity.Entities.SourceGen.SystemGenerator/Unity.Entities.SourceGen.SystemGenerator.SystemGenerator/Temp/GeneratedCode/Assembly-CSharp/JumpSys__System_12707194250.g.cs:92)
    8. Game.JumpSys+IFE_384976868_0+Enumerator.MoveNext () (at Unity.Entities.SourceGen.SystemGenerator/Unity.Entities.SourceGen.SystemGenerator.SystemGenerator/Temp/GeneratedCode/Assembly-CSharp/JumpSys__System_12707194250.g.cs:125)
    9. Game.JumpSys.OnUpdate (Unity.Entities.SystemState& state) (at Assets/Sources/World/Systems/JumpSys.cs:28)
    10. Game.JumpSys.__codegen__OnUpdate$BurstManaged (System.IntPtr self, System.IntPtr state) (at <62d0a0a36cd540a282971a4cc9704ec3>:0)
    11. (wrapper native-to-managed) Game.JumpSys.__codegen__OnUpdate$BurstManaged(intptr,intptr)
    12. Game.JumpSys+__codegen__OnUpdate_0000001B$BurstDirectCall.Invoke (System.IntPtr self, System.IntPtr state) (at <62d0a0a36cd540a282971a4cc9704ec3>:0)
    13. Game.JumpSys.__codegen__OnUpdate (System.IntPtr self, System.IntPtr state) <0x19c61b4e250 + 0x0005a> in <62d0a0a36cd540a282971a4cc9704ec3>:0
    14. (wrapper native-to-managed) Game.JumpSys.__codegen__OnUpdate(intptr,intptr)
    15. Unity.Entities.WorldUnmanagedImpl+UnmanagedUpdate_0000158C$BurstDirectCall.Invoke (System.Void* pSystemState) (at <12241d7570bd4d5cadb8ce5bcbeb0ab8>:0)
    16. Unity.Entities.WorldUnmanagedImpl.UnmanagedUpdate (System.Void* pSystemState) (at ./Library/PackageCache/com.unity.entities@1.0.11/Unity.Entities/WorldUnmanaged.cs:828)
    17. Unity.Entities.WorldUnmanagedImpl.UpdateSystem (Unity.Entities.SystemHandle sh) (at ./Library/PackageCache/com.unity.entities@1.0.11/Unity.Entities/WorldUnmanaged.cs:913)
    18. Unity.Entities.ComponentSystemGroup.UpdateAllSystems () (at ./Library/PackageCache/com.unity.entities@1.0.11/Unity.Entities/ComponentSystemGroup.cs:729)
    19. UnityEngine.Debug:LogException(Exception)
    20. Unity.Debug:LogException(Exception) (at ./Library/PackageCache/com.unity.entities@1.0.11/Unity.Entities/Stubs/Unity/Debug.cs:19)
    21. Unity.Entities.ComponentSystemGroup:UpdateAllSystems() (at ./Library/PackageCache/com.unity.entities@1.0.11/Unity.Entities/ComponentSystemGroup.cs:740)
    22. Unity.Entities.ComponentSystemGroup:OnUpdate() (at ./Library/PackageCache/com.unity.entities@1.0.11/Unity.Entities/ComponentSystemGroup.cs:693)
    23. Unity.Entities.SystemBase:Update() (at ./Library/PackageCache/com.unity.entities@1.0.11/Unity.Entities/SystemBase.cs:418)
    24. Unity.Entities.DummyDelegateWrapper:TriggerUpdate() (at ./Library/PackageCache/com.unity.entities@1.0.11/Unity.Entities/ScriptBehaviourUpdateOrder.cs:526)
    25.  
    26.  
     

    Attached Files:

    Last edited: Aug 10, 2023
  2. qcbf1

    qcbf1

    Joined:
    Nov 1, 2013
    Posts:
    19
    help, need me submit more info again?
    or what i missing?
     
  3. qcbf1

    qcbf1

    Joined:
    Nov 1, 2013
    Posts:
    19
    please help me... is that not the right thing to do?
     
  4. Tigrian

    Tigrian

    Joined:
    Mar 21, 2021
    Posts:
    105
    I did not forget you, i have debugged your problem during two hours, because i did not understood what was wrong with your problem (everything you are doing is right). It is just a simple mistake that is very hard to debug, but was actually in front of my eyes all of this time : Just remove the readonly from the OnUpdate of jumpSys.
     
  5. qcbf1

    qcbf1

    Joined:
    Nov 1, 2013
    Posts:
    19
    thank, thank very much.