Trying to assign entity parent.

So I’m in the process of converting my existing development to DOTS.

I have an entity that I’m calling a grid. This entity has an int value for an ID and follows a GameObjects transform values.

I also have a bunch of entities that I’m calling blocks that get creating during gameplay. These also have an int value for an ID. These are the entities I’m trying to parent to the grid entity.

The idea I am trying to achieve is that when a block is created it gets assigned an ID value. It then looks for the correct grid entity that matches that ID value and becomes a child of it.

This is my attempt so far.
Grid Entity IComponentData

using Unity.Entities;
using UnityEngine;

[GenerateAuthoringComponent]
public class GridBaseData : IComponentData {
    public int gridID = -1;
    public Transform gridTransform = null;
}

Grid Entity SystemBase

using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;

public class GridBaseSystem : SystemBase {
    protected override void OnUpdate() {
        Entities.ForEach((ref LocalToWorld localToWorld, in GridBaseData gridBaseData) => {
            var position = gridBaseData.gridTransform.position;
            var rotation = gridBaseData.gridTransform.rotation;

            localToWorld.Value = new float4x4(rotation, position);
        }).WithoutBurst().Run();
    }
}

Block Entity IComponentData

using Unity.Entities;
using UnityEngine;

[GenerateAuthoringComponent]
public class BlockBaseData : IComponentData {
    public int gridID = -1;
}

Block Entity SystemBase

using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;

public class BlockBaseSystem : SystemBase {

    protected override void OnCreate() {

        Entities.ForEach((Entity blockEntity, in BlockBaseData blockBaseData) => {

            float id = blockBaseData.gridID;
 
            Entities.ForEach((Entity gridEntity, in GridBaseData gridBaseData) => {
                if (id == gridBaseData.gridID) {
                    EntityManager.AddComponentData(blockEntity, new Parent() { Value = gridEntity });
                    EntityManager.AddComponentData(blockEntity, new LocalToParent());
                }
            }).WithStructuralChanges().Run();

        }).WithoutBurst().Run();
    }

    protected override void OnUpdate() {

    }
}

The grid Entity / GameObject pairing is working fine, I just think the code in the BlockBaseSystem OnCreate() is wrong and it’s not assigning the block entities to be children of the grid entity.
Can anyone help point out what I’m doing wrong here please.

Cheers
Matt

It is generally not a good idea to run an Entities.ForEach in OnCreate. Not everything is initialized at that point so what you are looking for may not exist. Debug.Log is still your friend in DOTS.

Aside from that you also cannot use Entities.ForEach inside another Entities ForEach.

The parent child relationship is also bidirectionnal. On the child you have the parent component and on the parent you should have the child buffer.
I’m not sure you need to assign both or if Unity picks it up and does the other link for you automaticaly.