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

Resolved SystemBase, establishing parent-child relationship for spawned entities

Discussion in 'Entity Component System' started by unslinga, May 12, 2021.

  1. unslinga

    unslinga

    Joined:
    Jul 12, 2017
    Posts:
    8
    I am trying to create a parent-child relationship with the prefabs I'm spawning into the scene with SystemBase.

    The error I am getting looks like this:
    ArgumentException: System.ArgumentException: A component with type:{0} has not been added to the entity.

    The code that's producing the error:
    Code (CSharp):
    1. var buf = commandBuffer
    2.     .AddBuffer<Child>(entityInQueryIndex, entity);
    3.  
    4. var instance = commandBuffer
    5.     .Instantiate(entityInQueryIndex, worldChunkData.prefab);
    6.  
    7. buf.Add(new Child { Value = instance });
    8.  
    9. commandBuffer.SetComponent(entityInQueryIndex, instance,
    10.     new Parent{ Value = entity });
    11. commandBuffer.SetComponent(entityInQueryIndex, instance,
    12.     new LocalToParent());
    Full stack trace:
    Code (CSharp):
    1. ArgumentException: System.ArgumentException: A component with type:{0} has not been added to the entity.
    2. Thrown from job: Unity.Entities.EntityCommandBuffer._mono_to_burst_PlaybackChainChunk
    3. This Exception was thrown from a function compiled with Burst, which has limited exception support. Turn off burst (Jobs -> Burst -> Enable Compilation) to inspect full exceptions & stacktraces.
    4. EntityCommandBuffer was recorded in ChunkSpawningSystem and played back in Unity.Entities.EndSimulationEntityCommandBufferSystem.
    5.   at (wrapper managed-to-native) System.Object.wrapper_native_0x1510352a0(intptr,intptr,Unity.Entities.EntityComponentStore/ArchetypeChanges&,Unity.Entities.ECBSharedPlaybackState&,intptr,int,int,bool,Unity.Entities.PlaybackPolicy)
    6.   at (wrapper delegate-invoke) <Module>.invoke_void_intptr_intptr_EntityComponentStore/ArchetypeChanges&_ECBSharedPlaybackState&_intptr_int_int_bool_PlaybackPolicy(intptr,intptr,Unity.Entities.EntityComponentStore/ArchetypeChanges&,Unity.Entities.ECBSharedPlaybackState&,intptr,int,int,bool,Unity.Entities.PlaybackPolicy)
    7.   at Unity.Entities.EntityCommandBuffer._forward_mono_PlaybackChainChunk (Unity.Entities.EntityDataAccess* mgr, Unity.Collections.LowLevel.Unsafe.UnsafeList* managedReferenceIndexRemovalCount, Unity.Entities.EntityComponentStore+ArchetypeChanges& archetypeChanges, Unity.Entities.ECBSharedPlaybackState& playbackState, Unity.Entities.ECBChainPlaybackState* chainStates, System.Int32 currentChain, System.Int32 nextChain, System.Boolean isFirstPlayback, Unity.Entities.PlaybackPolicy playbackPolicy) [0x00001] in /Users/chronos/Projects/DOTSJaffa/Library/PackageCache/com.unity.entities@0.17.0-preview.41/Unity.Entities/EntityCommandBuffer.interop.gen.cs:88
    8.   at Unity.Entities.EntityCommandBuffer.PlaybackChainChunk (Unity.Entities.EntityDataAccess* mgr, Unity.Collections.LowLevel.Unsafe.UnsafeList* managedReferenceIndexRemovalCount, Unity.Entities.EntityComponentStore+ArchetypeChanges& archetypeChanges, Unity.Entities.ECBSharedPlaybackState& playbackState, Unity.Entities.ECBChainPlaybackState* chainStates, System.Int32 currentChain, System.Int32 nextChain, System.Boolean isFirstPlayback, Unity.Entities.PlaybackPolicy playbackPolicy) [0x0000b] in /Users/chronos/Projects/DOTSJaffa/Library/PackageCache/com.unity.entities@0.17.0-preview.41/Unity.Entities/EntityCommandBuffer.interop.gen.cs:69
    9.   at Unity.Entities.EntityCommandBuffer.PlaybackChain (Unity.Entities.EntityDataAccess* mgr, Unity.Collections.LowLevel.Unsafe.UnsafeList* managedReferenceIndexRemovalCount, Unity.Entities.EntityComponentStore+ArchetypeChanges& archetypeChanges, Unity.Entities.ECBSharedPlaybackState& playbackState, Unity.Entities.ECBChainPlaybackState* chainStates, System.Int32 currentChain, System.Int32 nextChain, System.Boolean isFirstPlayback, Unity.Entities.PlaybackPolicy playbackPolicy) [0x000c9] in /Users/chronos/Projects/DOTSJaffa/Library/PackageCache/com.unity.entities@0.17.0-preview.41/Unity.Entities/EntityCommandBuffer.cs:2026
    10.   at Unity.Entities.EntityCommandBuffer.PlaybackInternal (Unity.Entities.EntityDataAccess* mgr) [0x00390] in /Users/chronos/Projects/DOTSJaffa/Library/PackageCache/com.unity.entities@0.17.0-preview.41/Unity.Entities/EntityCommandBuffer.cs:1829
    11.   at Unity.Entities.EntityCommandBuffer.Playback (Unity.Entities.EntityManager mgr) [0x00001] in /Users/chronos/Projects/DOTSJaffa/Library/PackageCache/com.unity.entities@0.17.0-preview.41/Unity.Entities/EntityCommandBuffer.cs:1696
    12.   at Unity.Entities.EntityCommandBufferSystem.FlushPendingBuffers (System.Boolean playBack) [0x00097] in /Users/chronos/Projects/DOTSJaffa/Library/PackageCache/com.unity.entities@0.17.0-preview.41/Unity.Entities/EntityCommandBufferSystem.cs:227
    13.  
    14. Unity.Entities.EntityCommandBufferSystem.FlushPendingBuffers (System.Boolean playBack) (at Library/PackageCache/com.unity.entities@0.17.0-preview.41/Unity.Entities/EntityCommandBufferSystem.cs:294)
    15. Unity.Entities.EntityCommandBufferSystem.OnUpdate () (at Library/PackageCache/com.unity.entities@0.17.0-preview.41/Unity.Entities/EntityCommandBufferSystem.cs:192)
    16. Unity.Entities.ComponentSystem.Update () (at Library/PackageCache/com.unity.entities@0.17.0-preview.41/Unity.Entities/ComponentSystem.cs:114)
    17. Unity.Entities.ComponentSystemGroup.UpdateAllSystems () (at Library/PackageCache/com.unity.entities@0.17.0-preview.41/Unity.Entities/ComponentSystemGroup.cs:472)
    18. UnityEngine.Debug:LogException(Exception)
    19. Unity.Debug:LogException(Exception) (at Library/PackageCache/com.unity.entities@0.17.0-preview.41/Unity.Entities/Stubs/Unity/Debug.cs:19)
    20. Unity.Entities.ComponentSystemGroup:UpdateAllSystems() (at Library/PackageCache/com.unity.entities@0.17.0-preview.41/Unity.Entities/ComponentSystemGroup.cs:477)
    21. Unity.Entities.ComponentSystemGroup:OnUpdate() (at Library/PackageCache/com.unity.entities@0.17.0-preview.41/Unity.Entities/ComponentSystemGroup.cs:417)
    22. Unity.Entities.ComponentSystem:Update() (at Library/PackageCache/com.unity.entities@0.17.0-preview.41/Unity.Entities/ComponentSystem.cs:114)
    23. Unity.Entities.DummyDelegateWrapper:TriggerUpdate() (at Library/PackageCache/com.unity.entities@0.17.0-preview.41/Unity.Entities/ScriptBehaviourUpdateOrder.cs:333)
    24.  
     
    Last edited: May 12, 2021
  2. StickyMitchel

    StickyMitchel

    Joined:
    Sep 2, 2019
    Posts:
    19
    It seems to me that the issue is that you try to add an entity to the buffer before it has been processed.
    You instantiate the Entity via the CommandBuffer so it's not available instantly.
    A solution that comes to mind is to instantiate it via the EntityManager (so you can add it to the buffer) or add a tag component to it so another system can pick it up later and add it to the buffer
     
  3. BrendonSmuts

    BrendonSmuts

    Joined:
    Jun 12, 2017
    Posts:
    86
    Looks like you are setting the parent component on “entity” instead of “instance”. Is this intentional? Seems like you would have the parent component on the child instance instead?
     
  4. unslinga

    unslinga

    Joined:
    Jul 12, 2017
    Posts:
    8
    I will tag it instead and do the link later then.

    This was accidental, it's supposed to be instance.
     
  5. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    939
    Hello,

    Maybe you could try using SetBuffer instead of AddBuffer (not sure what the difference is there)

    Also when working with buffer I prefer to work on them as soon as I get them, no instruction in between so first make the instance then add the buffer and set the child.

    That beeing said, when creating the instance with the command buffer, you will get a 'fake' entity reference so storing it in the child buffer in the following instruction will probably not give you the expected result (unless unity performs the remap when playing back the command buffer)

    As an alternative, have you tried to just set the parent, not the child ? maybe there is a a unity job that does the missing link for you.

    Final note, parent-child relationshipp will only affect transform calculation. It won't affect destroy/disable operation, for that you'll need to also add to the linked entity buffer.

    Edit final note 2 : turn of burst also to get more detailled error message, hten when it works you can turn it back on to check it's still working with it.
     
  6. unslinga

    unslinga

    Joined:
    Jul 12, 2017
    Posts:
    8
    The solution turned out to be much simpler than what I was trying to do in the first place. I had not found AddComponent in the API and was stuck using SetComponent.

    When I added parent with AddComponent everything else was solved automatically.

    Thank you for all the good feedback.
     
    StickyMitchel likes this.