How to remove object from scene with Three.js and JavaScript?

Sometimes, we want to remove object from scene with Three.js and JavaScript.

In this article, we’ll look at how to remove object from scene with Three.js and JavaScript.

How to remove object from scene with Three.js and JavaScript?

To remove object from scene with Three.js and JavaScript, we use the remove method.

For instance, we write

const removeEntity = (object) => {
  const selectedObject = scene.getObjectByName(object.name);
  scene.remove(selectedObject);
  animate();
};

to define the removeEntity function.

In it, we call scene.getObjectByName to get the object by the name of the object.

Then we call scene.remove with selectedObject to remove it.

Finally we call animate to animate the changes.

Conclusion

To remove object from scene with Three.js and JavaScript, we use the remove method.