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 Creating additional entity in Baker

Discussion in 'Entity Component System' started by andywatts, Feb 4, 2023.

  1. andywatts

    andywatts

    Joined:
    Sep 19, 2015
    Posts:
    107
    Hi,

    I'm trying to create an additional entity and add a reference to it...
    The reference becomes Entity.Null after baking.

    Is there different pattern I should be using?
    Maybe there's an ECB I should be using to create the additional entity?

    Code (CSharp):
    1. class BobAuthoring : MonoBehaviour{}
    2.  
    3. class BobBaker : Baker<BobAuthoring>
    4. {
    5.     public override void Bake(BobAuthoring authoring)
    6.     {
    7.         EntityManager em = World.DefaultGameObjectInjectionWorld.EntityManager;
    8.         var jim = em.CreateEntity();
    9.         AddComponent(new Bob{ MyEntity = jim } );   // <--- MyEntity will be Entity.Null after bake.
    10.     }
    11. }
    12.  
    13. public struct Bob : IComponentData
    14. {
    15.     public Entity MyEntity;
    16. }
     
  2. andywatts

    andywatts

    Joined:
    Sep 19, 2015
    Posts:
    107
    Solved.

    Code (CSharp):
    1. class BobBaker : Baker<BobAuthoring>
    2. {
    3.     public override void Bake(BobAuthoring authoring)
    4.     {
    5.         var jim = CreateAdditionalEntity();
    6.         AddComponent(new Bob{ MyEntity = jim } );
    7.     }
    8. }