Delete or destroy an object (mesh) in three.js?

I have added some mesh elements using for loop. then added that in the scene. Now I need to select it and i have a button to delete in dat.gui . So i need to delete that through that button. I use following lines to delete

if(currently_selected_object !== null) {
    scene.remove(currently_selected_object);
    currently_selected_object.geometry.dispose();
    currently_selected_object.material.dispose();
    console.log('delete');
    animate();
}

currently selected object is defined on intersect using mouse. After selecting an object. I clicked on the delete button. It is just removed from the scene. But it is available in it’s place as invisible. If I select in that place. it shows following error.
“Uncaught TypeError: Cannot read property ‘matrixWorld’ of null.”. So I can’t select the object which was behind to the deleted object. Please help me

Try removing the object from the scene after you disposed instead of before ?

I’m afraid it’s not possible to help you unless you demonstrate the issue with a live example.

1 Like

Actually I got answer. We need to made the material and geometry of the mesh as “undefined”, Then remove from scene. This is the perfect way for delete a mesh .

Um, this should actually not be necessary. Seems there is a problem in your app.

1 Like

Maybe @Mugen87 . But Now it is working properly