Creating entities at runtime: Any entity I make a child immediately unparents the following frame

Hi all,

I am foregoing the baking process and am creating and managing my entities at runtime to facilitate a hybrid approach. This is serving me well so far, but like the title says, whenever I give an entity the Parent component it automatically un-parents itself in the following frame. The parent-child relationship shows correctly before then, adding the Child buffer to the parent as expected, and appearing correct in the Hierarchy view.

I thought it might be because I gave both parent and child a collider and rigidbody, but it even happens on plain entities. Does anyone know what might be causing this?

Unity 6000.0.0b11, Entities 1.2

Thanks <3

I’m not in Unity 6 but I’m also using Entities 1.2. May I ask for the code that managed your entity (where you add components, the Parent component in particular)?

Of course, thank you for taking a look! For completeness’ sake I’ve included the full file just in case, but here’s the relevant method:

void CreateEntity(EntityHost parentHost) {
        Entity = entityManager.CreateEntity();
        entityManager.SetName(Entity, $"[Hosted] {entityName} ({transform.name})");

        entityManager.AddComponentData(Entity, new LocalTransform {
            Position = transform.position,
            Rotation = transform.rotation,
            Scale = 1f,
        });

        IDynamicAuthoring[] dynamicAuthorings = transform.GetComponents<IDynamicAuthoring>();
        foreach(var authoring in dynamicAuthorings) {
            if(((MonoBehaviour)authoring).enabled) {
                authoring.Bake(this);
            }
        }

        if(parentHost != null) {
            Debug.Log($"Adding {parentHost.name} as parent to {transform.name} ");

            AddComponentData(new Parent { Value = parentHost.Entity });

            Debug.Log(entityManager.GetComponentData<Parent>(Entity));
        }
    }

9834627–1414707–EntityHost.cs (4.31 KB)

Thanks for the script! Right off the bat:

  • Did you test the while loop in the BuildChildEntityMap? It might cause a bug because it’s not changed inside the loop. This can be converted to a better recursion method instead.
  • I don’t have a safe scene to test this but, I quickly dropped it in a scene and immediately received an error with the entityManager. You don’t need to cache the EntityManager for your use case.

The building of the entities is working as expected. If I pause the editor before hitting Play, I can see all the right entities, with all of their correct components and relationships. The problem is that the parent-child relationship is immediately broken the following frame, and I can’t for the life of me figure out why.

You’re right on the caching though; made it a convenience getter instead. (I’ll still need it after Awake() as the hosts are pooled)

AddComponentData(Entity, new Parent { Value = parentHost.Entity });

Are you missing the Entity part here?

I exposed some methods with identical names in my EntityHost MB to more easily allow my runtime “bakers” to add components. So good catch, but nope!

ParentSystem code suggests LocalToWorld is needed for the hierarchical entities. Try adding that.

Dang, I was hopeful for a second. But no dice, it still un-parents after 1 frame

Did you add LocalToWorld on the parent?

3 Likes

…I did not. Great catch, thank you so much! I can sleep easy tonight haha

1 Like