Stefano Maglione Stefano Maglione - 1 year ago 162
Javascript Question

Threejs remove all together object from scene

I tried to make a function to remove all object from scene in one shoot but it removes only one object for invocation.

GeometryModel.prototype.clearScene = function(scene) {
var i;
for(i=0; i < scene.children.length; i++){
obj = scene.children[i];
scene.remove(obj);
}
}


another solution I tried and that works is this:

scene.children={};


but I am not sure if it is correct.

Answer Source

You have to do the opposite:

for( var i = scene.children.length - 1; i >= 0; i--) { }

because in each iteration the .children array changes once you do a .remove() from the start and the indexing of that array changes.

If you want to understand it better, unroll the for loop and follow what the index into the array is.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download