Search Unity

Bug EntityCommandBuffer.Instantiate fails silently when assigning a component referencing another entity

Discussion in 'Entity Component System' started by Wolfos, Aug 26, 2022.

  1. Wolfos

    Wolfos

    Joined:
    Mar 17, 2011
    Posts:
    951
    The relevant code is essentially this:
    EDIT:
    Code (CSharp):
    1. var commandBuffer = new EntityCommandBuffer(Allocator.TempJob);
    2.  
    3. var query = GetEntityQuery(typeof(OtherComponent));
    4. otherEntity = query.GetSingletonEntity(); // otherEntity is assigned as expected, and it's components are used in the job without issue
    5. Entities
    6.     .WithoutBurst()
    7.     .WithAll<TypeToIterateOn>()
    8.     .ForEach((Entity entity, ref Translation translation, ref TypeToIterateOn x) =>
    9.     {
    10.         var newEntity = commandBuffer.Instantiate(prefab);
    11.         commandBuffer.SetComponent(newEntity, translation); // Works
    12.         var newComponent = new NewComponentType
    13.         {
    14.             Source = entity, // Works fine
    15.             Target = otherEntity // This causes the new entity not to instantiate
    16.         };
    17.  
    18.          commandBuffer.AddComponent(newEntity, newComponent);
    19.     }).Run();
    20.  
    21. commandBuffer.Playback(EntityManager);
    22. commandBuffer.Dispose();
    I'm trying to Instantiate an entity from a prefab, and add a new component with the data I set.
    Instantiating works fine, I can set the data on Translation component, I can add the component, but as soon as I try to set the data the entity completely fails to instantiate. There is no error or exception.

    It works with all the data in the component, but not if I set the entity I got via the query earlier (which exists).
     
    Last edited: Sep 1, 2022
  2. Wolfos

    Wolfos

    Joined:
    Mar 17, 2011
    Posts:
    951
    There's definitely a bug here - at the very least it shouldn't fail silently.
     
  3. Arnold_2013

    Arnold_2013

    Joined:
    Nov 24, 2013
    Posts:
    285
    I would expect this to work. As long as the "otherEntity" fully exists at that point in your 'relevant code'. With ECB an instantiated or created entity is not fully available until after the playback.
    [SetComponent in line 20 will only work if the prefab has the component. But I assume this case is oke. Since Add would always work]

    Maybe run it in debug mode and step through the code to see where it does not work as expected... Or update your example to a real version and see if it really does not work.
     
  4. Wolfos

    Wolfos

    Joined:
    Mar 17, 2011
    Posts:
    951
    Since it's a command buffer, everything's supposed to happen all at once when I call Playback so debugging doesn't do much. I do know that otherEntity exists and points to the expected entity.
    If I remove the line where I assign otherEntity to the data, it works as expected. If not, the entity simply won't instantiate.
     
  5. Arnold_2013

    Arnold_2013

    Joined:
    Nov 24, 2013
    Posts:
    285
    Yeah, I agree its weird. If you are in .Run() with your real code, you could skip the ECB and work directly on the EntityManager and use it to instantiate + add components. I would not expect the result to be different...
    Or it is a real bug and you want it fixed, I guess you could file a bug report.