[SOLVED] Cloned entities not appearing

Here is my demo project:
https://playcanvas.com/editor/scene/1146914

I simply have a cube which I reference in cloneEntity.js. I clone the entity and position it. The getPosition() reads correctly but it does not show up in the scene. Checked material and mesh as well, they appear to not be null. What’s going on? Why can’t I see the cloned object? I must be missing something obvious …

Here is my code:

var CloneEntity = pc.createScript('cloneEntity');
CloneEntity.attributes.add('sourceEnt', { type:'entity' });

CloneEntity.prototype.initialize = function() {
    var clone = this.sourceEnt.clone();
    console.log("source pos:"+this.sourceEnt.getPosition());
    console.log("clone pos 1:"+clone.getPosition());

     clone.setPosition(0,0.5,2);
     console.log("clone pos 2:"+clone.getPosition()); // returns [0,0.5,2]

};

thanks for any help
Charlie

Update. Apparently you need to add the clone to app root, and also enable it after setPosition. Here’s the working code:

var CloneEntity = pc.createScript('cloneEntity');
CloneEntity.attributes.add('sourceEnt', { type:'entity' });

CloneEntity.prototype.initialize = function() {
    var clone = this.sourceEnt.clone();
    console.log("source pos:"+this.sourceEnt.getPosition());
    console.log("clone pos 1:"+clone.getPosition());
    this.app.root.addChild(clone);
    clone.setPosition(0,0.5,2);
    clone.enabled = true;
};

Hi @Charlie_Van_Norman! Can you try to add the line below?

this.app.root.addChild(clone);
1 Like

This works! I did not realize you needed to add it somewhere. That’s not clear from the docs
https://developer.playcanvas.com/en/api/pc.Entity.html#clone

(I assumed you only needed to “add” it there if you wanted it to be a sibling, but actually adding is mandatory)

Thanks!

1 Like