Whenever I select a 3D model, my goal is to retrieve the object's ID, name, or the actual object itself in order to effectively delete it.
function onDocumentMouseDown( event ) {
if (enableRaycasting){
event.preventDefault();
mouse.set( ( event.clientX / window.innerWidth ) * 2 - 1, - (
event.clientY / window.innerHeight ) * 2 + 1 );
raycaster.setFromCamera( mouse, camera );
var intersects = raycaster.intersectObjects( scene.children, true );
console.log(intersects);
if ( intersects.length > 0 ) {
scene.remove(intersects[0]); //doesnt work
scene.remove(intersects[0].object); //doesnt work
scene.remove(intersects[1]); //doesnt work
scene.remove(intersects[1].object); //doesnt work
console.log(intersects[0].object.name) //Bot_Skinned_0
console.log(intersects[0].object.id)// output: 72
}
render();
}
}
Situation: Upon creating the object, its initial ID is 21 and the model's name is "BotSkinned" (as specified by object.name = "BotSkinned"
). Subsequently, adding the object to the scene results in:
scene.add( object );
console.log(scene.getObjectByName("BotSkinned").id); //output: 21
Using Raycasting, my aim is to accurately target that specific object and potentially remove it. Unfortunately, none of the removal commands seem to be effective, and discrepancies between the assigned name and ID become evident. Does anyone have a solution for obtaining the correct object.id
? This would offer a straightforward resolution.
Edit: While simply using
scene.remove(scene.getObjectByName("BotSkinned"))
could suffice, the ideal scenario involves the raycast precisely identifying the desired object among many others in the future instances.