Sometimes while loading the importing the glb it gives an error but after page refresh error not showing.
can anyone help me out, how to fix that issue.
BJS Unable to load from /xxxxpath/scenes/RR_310_Red.glb:
Error in onSuccess callback:
TypeError: Cannot read properties of undefined (reading '0')
function redBikeMeshLoad(scene, shadowGenerator) {
// red bike RR_310_Red loaded
BABYLON.SceneLoader.ImportMesh(
"",
"./assets/RR310/scenes/",
"RR_310_Red.glb",
scene,
function (meshes) {
red_bike_meshes = meshes;
mesh = meshes[0];
// shadow generator
shadowGenerator.addShadowCaster(mesh);
// default bike select
rootMeshEnabled("red");
}
);
}
function rootMeshEnabled(bikeName = "red") {
if (bikeName === "red") {
red_bike_meshes[0].setEnabled(true);
const redGroup1Chidren = red_bike_meshes[0].getChildren();
redGroup1Chidren.forEach((e) => {
if (e.name === "group1") {
e.setEnabled(false);
}
});
black_bike_meshes[0].setEnabled(false);
} else if (bikeName === "black") {
red_bike_meshes[0].setEnabled(false);
black_bike_meshes[0].setEnabled(true);
const blackGroup1Children = black_bike_meshes[0].getChildren();
blackGroup1Children.forEach((e) => {
if (e.name === "group1") {
e.setEnabled(false);
}
});
}
}
Then I think this is the issue: you are not waiting for your both meshes to be completly loaded, so sometimes works sometimes no.
I would recomand you to use ImportMeshAsync instead. Or load your second mesh in the OnSuccess callback function of first mesh, and handle them both in the second OnSuccess function. Here are some examples