[SOLVED] HighlightLayer + onNewMeshAddedObservable issue on babylonjs 4

#1

Hi,

found weird bug in babylonjs 4
here is playground

there

scene.onNewMeshAddedObservable.add(function (mesh) {
            if (mesh instanceof BABYLON.Mesh) {
                highlight.addExcludedMesh(mesh);
            }
    });

is called before mesh gets props so it crashes with random errors,
usually with Cannot read property '_onBeforeBindObservable' of undefined,
because _internalMeshDataInfo is still undefined

I am doing something wrong ? :slight_smile:

Thank you

#2

Ok looks like its expected as observable is triggered in AbstractMesh constructor so Mesh constructor is not finished at that point

#3

this is correct. Best solution is to call your code inside setTimeout(func, 0)

#4

Thank you @Deltakosh,
yes thats what I ended up

scene.onNewMeshAddedObservable.add(mesh => {
            if (mesh instanceof Mesh) {
                Tools.SetImmediate(() => {
                    code....
                });
            }
        });
1 Like