So I am making a survival game and I want trees to randomly spawn. I am just trying to clone one time but for some reason it clones multiple times.
Here is my code
var Addtrees = pc.createScript('addtrees');
// initialize code called once per entity
Addtrees.prototype.initialize = function() {
var tree = this.entity;
var treeClone = tree.clone();
treeClone.setPosition(0, 5, 0);
this.app.root.addChild(treeClone);
};
// update code called every frame
Addtrees.prototype.update = function(dt) {
};
When this entity is created, the first thing it does (because it’s in initialize) is to clone itself, then the clone does the same thing and it’s clone will do the same thing, etc.
You basically have a recursive loop.
To fix this, don’t clone itself in the initialize function.
What’s the intention/outcome here? Without more context, my suggestion would be to do this from another script that is not on the entity you are trying to clone.