Howto use getMeshByName with a GLTF

I’m looking to create the floormesh array from a GLTF, but get the following error when trying to assign by name:

ERROR in src/BabylonExamples/LightsShadows.ts:65:51
TS2345: Argument of type ‘Nullable’ is not assignable to parameter of type ‘AbstractMesh’.
Type ‘null’ is not assignable to type ‘AbstractMesh’.
63 |
64 | // Add some floor tiles to the floorMesh for teleportation

65 | defaultXRExperience.teleportation.addFloorMesh(this.scene.getMeshByName(“FLOOR1”) );
|

I’d open the inspector and check the name actually exists in the scene and the GLTF is fully loaded before you’re making the call to getMeshByName() - it’s possible you have a race condition.

Also, add a null check before the call:

const floorMesh = this.scene.getMeshByName(“FLOOR1”);
if (floorMesh) {
  defaultXRExperience.teleportation.addFloorMesh(floorMesh);
} else {
  console.log("Can't find floor mesh");
}
1 Like