Object3D.getObjectById() not working

My goal is to click on an object and identify it’s parent group by it’s distinct uuid. Then I want to pass that uuid to a remove function to remove that group from the scene. All seems well except the getObjectById() call is returning ‘undefined’. I’m new to Three.js. Can anyone spot what I may be doing wrong? Thanks in advance.


//function to determine clicked items' partent group uuid
function onMouseUp(e) {
console.log('clicked once');
raycaster.setFromCamera(mouse, camera);
intersects = raycaster.intersectObjects(leftwallgrp.children, true);

if (intersects.length > 0){
    var clickedItemID = intersects[0].object.uuid;
    var clickedItemName = intersects[0].object.name;
    var clickedObjectParent = intersects[0].object.parent.name;
    var clickedObjectParentID = intersects[0].object.parent.uuid;
    var clickedObjectParentGrp = intersects[0].object.parent.parent.name;
    console.log('You single clicked: ' + clickedItemName); // shows correct name
    console.log('UUID is: ' + clickedItemID); // shows correct uuid
    console.log('parent is:' + clickedObjectParent); // shows correct name
    console.log('parent UUID is:' + clickedObjectParentID);  //shows correct uuid   
// function to find the group by uuid and remove from scene
function remove(id) {
var removeObject = scene.getObjectById(id, true);
console.log(removeObject); // returns 'undefined'
scene.remove(removeObject); // no action since removeObject is undefined
console.log('object id to remove is:' + id); // this shows the correct uuid

Also here is the console output:

clicked once
structure.js:1378 You single clicked: 9 Light Door 3x6
structure.js:1379 UUID is: B451C1E4-66FF-494B-B24F-380B35C6F9E5
structure.js:1380 parent is:leftwall=door9light
structure.js:1381 parent UUID is:5BBE3E3D-5123-4349-B08A-F98713B2A24A
structure.js:1481 undefined
structure.js:1483 object id to remove is:5BBE3E3D-5123-4349-B08A-F98713B2A24A

Is it because the uuid contains letters perhaps? If so, any suggestions on creating a simple unique username for each group? Maybe use getObjectByName()?

That’s how .getObjectById() works under the hood:

1 Like

Thanks prisioner849. That helped clarify my error. I was comparing apples to oranges. The uuid and the id are two totally different items. For some reason I was thinking they were the same. Grabbing the object by it’s Id worked well. I changed the following:

var clickedObjectParentID = intersects[0].object.parent.id;

Now it’s working.

Thank you for your time.