Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove child objects from Object3D

If I create objects in the following way:

var group = new THREE.Object3D();

for (var i = 0; i < 10; i++) {

    geometry = new THREE.BoxGeometry(1, 1, 1);
    material = new THREE.MeshNormalMaterial();
    mesh = new THREE.Mesh(geometry, material);

    group.add(mesh);

}

scene.add(group);

How, then, do I remove those objects from that group?

I tried doing this...

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

    scene.remove(group.children[i]);

}

...but it outputs as 'undefined'. What am I doing wrong here?

like image 402
jasfar Avatar asked Apr 18 '16 23:04

jasfar


3 Answers

for (var i = group.children.length - 1; i >= 0; i--) {
    group.remove(group.children[i]);
}
like image 157
Yellow and Red Avatar answered Nov 19 '22 18:11

Yellow and Red


In one line you can do this.

  group.remove(...group.children);
like image 6
George C. Avatar answered Nov 19 '22 16:11

George C.


You can use

object.children = [];
like image 1
rvcristiand Avatar answered Nov 19 '22 16:11

rvcristiand