Jacob Truax
Jacob Truax

Reputation: 77

Problems with raycasting and onMouseClick to remove a mesh from three.js scene

I am trying to remove 4 meshes from my three.js scene when I click on them. I can't seem to figure out what the problem is. I am able to change the geometry material but when I try to remove them it doesn't work. Here is my raycasting code below

var raycaster = new THREE.Raycaster();
var mouse = new THREE.Vector2();

function onMouseClick( event ) {

    mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
    mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;

    raycaster.setFromCamera( mouse, camera );

    var intersects = raycaster.intersectObjects([a, b, c, d]);

    for ( var i = 0; i < intersects.length; i++ ) {

    if (intersects[ i ].object.geometry.type == "PlaneGeometry") {
            intersects[ i ].object.parent.remove(intersects[ i ].object);
        }
    }

}

Upvotes: 0

Views: 899

Answers (1)

pink li
pink li

Reputation: 142

It might be intersects[ i ].object.parent is null, so no removing happens.

Upvotes: 0

Related Questions