I was trying to iterate over all my grids via
EntityManager.GetAllUniqueSharedComponentData(this.grids);
when I found out that there are 2 unique grids existing.
The grid component that I create intentionally by
this.gridComponentData = new GridComponent()
{
Size = 3,
CellSwizzle = this.Grid.CellSwizzle,
Root = this.gridRootEntity
};
Manager.SetSharedComponentData(cellEntity, this.gridComponentData);
…and another grid which is created with default values (e.g. Size = 0).
I tried Logging all existing grids and how many entities are referencing them:
this.grids.Clear();
EntityManager.GetAllUniqueSharedComponentData(this.grids);
foreach (var grid in this.grids)
{
var gridQuery = EntityManager.CreateEntityQuery(typeof(GridComponent));
gridQuery.SetSharedComponentFilter(grid);
var gridEntities = gridQuery.ToEntityArray(Allocator.TempJob);
Debug.Log($"Grid: Entities={gridEntities.Length} Size={grid.Size}");
gridEntities.Dispose();
}
The output is “Grid: Entities=0 Size=0” which implies that there are no entities that reference this Grid SC.
From my understanding that should be impossible as SCs are reference counted and should be destroyed when unused.
I was wondering if just having a private field of the SharedComponent type is enough for the “empty” SC to be created but testing with a simple MonoBehaviour didnt have the same result:
public class SharedComponentTest : MonoBehaviour
{
private GridComponent TestGrid;
}
This results in 2 questions:
- Why is this “empty” SharedComponent created in the first place?
- Why does it (still) exist when it is not referenced anywhere?
Additional context:
- I am instantiating entities in edit and play mode so it might be created during edit mode and just “stay”