How do I get a child (by name or id) of imported file?

Hi, I imported a glb file and need to access one of its childs to assign a trigger. It works fine for the first object (let’s call it sphereH).

var assignATrigger = scene.getMeshByName("sphereH");

When I load another file and also need to access the child of that, which happens to have the same name is not activating the trigger. I think scene is giving me back first sphereH, or in any case it doesn’t work like that.

Is there something like scene.objectA.getMeshByName(“sphereH”); ?

Since mesh names are not necessarily unique scene.getMeshByName will only get the first in the meshes array. If you made the files you will need to rename them. Otherwise you will need to find and record their array index and then use that as a reference in you project code.

Thanks JohnK, I think I can’t rename as those object are loaded with different number (could be 1 object or 30, for example). Second option sounds better, In inspector the hierarchy is:

  • root
    • mesh01
    • mesh02
    • sphereA

Can I get the array index with that information?

1 Like

scene.meshes should give you what you want.

1 Like