Broken baker with entities 1.0 when using 2 string fields in a class Component

Use case

I have :

  • A baker creating a Map
  • A baker creating an additionnal entity referencing the Map (PrimaryEntity)
  • This additionnal entity have string metadata to load the map

Issue

At first, when I was using a single string metadata in my IComponentData, everything as fine, in the system the reference to the Entity is working as expected:

BUT after I added a second string to a class IComponentData, at runtime, the entity referenced is completly messed up, is this exemple the entity is a System instance AND NOT the Cube entity:

(screen shots based on code below):

How to reproduce

  • Create a new scene with an empty subscene

  • Add a new Cube to the subscene

  • Add the DebugWrongEntityAuthoringComponent component to the cube

using Unity.Entities;
using UnityEngine;

[DisallowMultipleComponent]
public class DebugWrongEntityAuthoringComponent : MonoBehaviour
{
    public class Baker : Baker<DebugWrongEntityAuthoringComponent>
    {
        public override void Bake(DebugWrongEntityAuthoringComponent authoring)
        {
            AddBuffer<MapData>();
            var mapEntity = GetEntity();

            var extraWorkingEntity = CreateAdditionalEntity();
            AddComponentObject(extraWorkingEntity, new LoadMyMapWorking { MyMap = mapEntity });

            var extraBuggyEntity = CreateAdditionalEntity();
            AddComponentObject(extraBuggyEntity, new LoadMyMapBuggy { MyMap = mapEntity });
        }
    }
}

public struct MapData : IBufferElementData
{
    public int DummyData;
}

public class LoadMyMapWorking : IComponentData
{
    public Entity MyMap;
    public string StringData1;
}

public class LoadMyMapBuggy : IComponentData
{
    public Entity MyMap;
    public string StringData1;
    public string StringData2;
}

[RequireMatchingQueriesForUpdate]
public partial class LoadMyMapSystem : SystemBase
{
    protected override void OnUpdate()
    {
        var ecb = this.GetSingleton<EndSimulationEntityCommandBufferSystem.Singleton>().CreateCommandBuffer(World.Unmanaged);
        var getBuffer = GetBufferLookup<MapData>(isReadOnly: false);

        Entities
             .WithoutBurst()
             .ForEach((LoadMyMapWorking loadMyMap, in Entity loadEntity) =>
             {
                 ecb.DestroyEntity(loadEntity);
                 var myData = getBuffer[loadMyMap.MyMap];
                 myData.Add(new MapData());
                 Debug.Log("LoadMyMapWorking.MyMap is working as expected");
             })
             .Run();

        Entities
             .WithoutBurst()
             .ForEach((LoadMyMapBuggy loadMyMap, in Entity loadEntity) =>
             {
                 ecb.DestroyEntity(loadEntity);
                 try
                 {
                     var myData = getBuffer[loadMyMap.MyMap];
                     myData.Add(new MapData());
                 }
                 catch (System.Exception e)
                 {
                     // Oopsy!! :
                     // A component with type:MapData[Buffer] has not been added to the entity.Entities Journaling may be able to help determine more information.
                     // Please enable Entities Journaling for a more helpful error message.
                     Debug.LogError("LoadMyMapBuggy.MyMap is NOT working as expected: ");
                     Debug.LogError(e.Message);
                 }
             })
             .Run();
    }
}

Context

Unity Version:
2022.2.0b14.112.4874

Packages versions

Thank you for the report. I was able to repro and I’m working on the fix.

In the mean time, you can get around this issue by declaring the Entity field last in the class component.

Thank you, I confirm the workaround works!