jasfar
jasfar

Reputation: 197

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?

Upvotes: 17

Views: 17387

Answers (3)

rvcristiand
rvcristiand

Reputation: 452

You can use

object.children = [];

Upvotes: 2

user6600549
user6600549

Reputation:

In one line you can do this.

  group.remove(...group.children);

Upvotes: 11

Yellow and Red
Yellow and Red

Reputation: 705

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

Upvotes: 26

Related Questions