Error trying to get clone's child

I have an entity who have a child, i use
this.myEntity.children[0].name
And i find what i expect. But then i clone this entity and try

clone.children[0].name
And i got “untitled” in return as if there was no childrens.

Any ideias on what could i be doing wrong?

Hmm… That looks like a bug if the name isn’t copied. Could you create a reproducible code project/sample that showcases this?

Can you link to a scene that reproduces this problem?

You may have encountered a graphnode (invisible in editor) these are added by playcanvas sometimes. Check if the node you are trying to access is in fact a pc.Entity.

Have a look here: https://github.com/playcanvas/engine/issues/1035

I’ve made a simples project to reproduce it.
https://playcanvas.com/editor/scene/648542

All the code i used is this…

var CloneChild = pc.createScript('cloneChild');

CloneChild.attributes.add('entityWithAChild', {type: 'entity'});

CloneChild.prototype.initialize = function() {
    
    var clone = this.entityWithAChild.clone();
    
    console.log("entity = " + this.entityWithAChild.children[0].name);
    console.log("clone  = " + clone.children[0].name);
};

Thanks for the repro.

I’m not entirely convinced that this is a true bug because I don’t think the engine makes any claims about expected order of the children array on a pc.Entity. However, it’s fairly easy to change pc.Entity#clone to ensure the cloned hierarchy’s children arrays have the same order:

If @dave approves the PR, I’ll merge it.

But just to emphasize, I don’t think you should write code that depends on an entity being at a given index in the children array. Instead, pc.Entity#findByName (or entity script attributes) is probably safer.

2 Likes