[SOLVED] Entity Cloning Script Not Functioning

Hey guys,

I am trying to clone entities every second in a game of mine. However, whenever I run, this happens.

My game can be found at https://launch.playcanvas.com/870258?debug=true

My script is as follows

/* jshint esversion: 6*/

var CloneFallingObjects = pc.createScript('cloneFallingObjects');

// initialize code called once per entity
CloneFallingObjects.prototype.initialize = function() {
    this.timer = 0;

// update code called every frame
CloneFallingObjects.prototype.update = function(dt) {
    this.timer += dt;
    let object_array = ["GreenObject", "RedObject", "BlueObject"];
    let position_array = [(-2.017, 7.573, 0.035), (-0.318, 7.594, 0.035), (1.404, 7.615, 0.111)];
        let newEntity = new pc.Entity();
        let number = Math.floor(Math.random() * (3 - 1 + 1)) + 1;
        let number2 = Math.floor(Math.random() * (3 - 1 + 1)) + 1;
        newEntity = this.entity.findByName(object_array[(number - 1)].toString()).clone();
        newEntity.setLocalPosition(position_array[(number2 -1)]);
        newEntity.enabled = true;
    }.bind(this), 1000);

Sorry if there are any formatting errors; I’m on mobile rn. Please help me out with this.

You should debug this line, most likely the findByName method doesn’t manage to find the entity you would like to clone. So the .clone() correctly is not defined.

Also you should be careful of cloning entities in the update method of your script, you will get quickly thousands of new entities (one per frame) that can slow down or even crash your app.

I will move it to the initialize function. But, if I run the line alone, without the clone call, it works perfectly.

It works you mean, it clones/adds and an entity? Or just isn’t throwing an error? Because that’s different.

Also, your code for getting a random index of the array is flawed. It produced a range of 1-3, which will ignore your first element and then break when it tries to find the element with index 3 (arrays are 0-indexed) :slight_smile: Either subtract 1 from that calculation or just use Math.floor(pc.math.random(0, 3)); or better yet, Math.floor(pc.math.random(0, object_array.length));

EDIT: i see you already subtract 1 from that calculation, my bad.


And another note, your code won’t clone a new entity every second, it’ll clone one every frame with a delay of a second. Either use setInterval() in your initialize function or use a looping tween (nicer because they’ll get paused when the tab is inactive or timeScale is 0


this.app.tween({}).to({}, 1.0, pc.Linear).loop(true)
   .on('loop', function () {
       // spawning code here
    }, this);

throw that in your initialize function, too.

1 Like

Thanks a lot @davidpox and @Leonidas.

It returns the value of the entity.

There isn’t an Entity named BlueObject in the scene which is why findByName is returning null when number is 2

Edit: Fixed version of the project: https://playcanvas.com/editor/scene/874792

1 Like

Thanks for the assistance @yaustar.