You need to go back to the front, not front to back, when deleting array objects like this.
var obj, i; for ( i = scene.children.length - 1; i >= 0 ; i -- ) { obj = scene.children[ i ]; if ( obj !== plane && obj !== camera) { scene.remove(obj); } }
What happens when you delete a node, everything after it is shifted. Say you delete scene.children [0]: children [1] will become new 0, 2 will become 1, etc. When moving from 0 to array.length, the for loop already moves and skips 1 node for each one you delete.
As an added plus, this should go a little faster, especially if you have a lot of objects, since scene.children.length gets only once, and not every loop.
Crazycatz
source share