Scripterror: element.getMeshById(Mesh) is null

Hey,

i develop a Function which should hide all Meshes. There are also 1 Function which hide a Mesh by clicking on it.

Now in the new Function which should hides all Meshes i iterate through a for-loop and choose in every loop the needed region and elements of the Mesh. Now i get everytime at **this.scene.getMeshByID(this.regions["region"].id)** a null returned. I don’t know why.

I have also the 1 other Function which hide a Mesh by clicking on it. There all works fine. There are the same values like in the new Function. But in the new Function it don’t works. I have debugging and testing now a lot but don’t find the Error…

This is my Code:
regions = {
kamera : { id: ‘Kamera’, alpha: 6.153267413154244, beta: 1.3094542662847675, radius: 20 },
motorschutz : { objects: {object1: ‘Motorschutz Groß’, object2: ‘Motorschutz Groß.001’, object3:‘Motorschutz Groß.002’, object4:‘Motorschutz Groß.003’}, alpha: 5.398361953174415, beta: 1.648784279856813, radius: 20 }
}

hideAllMeshes = () => {

  let objects, objectMesh;
  
  for (let region in this.regions){

    objects = this.regions[region].objects;

    if(objects == undefined){
      let object = this.scene.getMeshByID(this.regions[region].id);
      object.setEnabled(false);
    }else{

      for(let object in objects){
        objectMesh = this.scene.getMeshByID(this.regions[region].objects[object])
        objectMesh.setEnabled(false);
      }
    }  
  }
}

//Build the scene when the component has been loaded.
componentDidMount() {
  this.setEngine()
  this.setScene()
  this.setCamera()

  this.loadModels().load()

  this.hideAllMeshes();
}

Have anyone an Idea why it don’t works?

I think this.loadModels().load() is asynchronous?

It would mean the scene is probably not yet populated with your meshes when hideAllMeshes is called. Make sure the scene is finished loading before calling hideAllMeshes.

1 Like

Hey,

Thanks for you answer.

Yes after testing i have seen, that the Meshes aren’t avaible at the Time where i execute the hideAllMeshes() Function. So i using the Function setTimeout with 1 Second. After 1 Second all Meshes are avaible and the Function get executed.

Be aware that this 1s could be different depending on the computer where the code is executed…

You should instead use the callback provided on the “on loaded” event of the different loading methods (SceneLoader.Append / ImportMesh / …).

1 Like

Hey,

i have looked now after the Functions which get executing after the Model got full loaded.

I can’t find anything which works at me. Maybe i search after the false.

Can you please share with me the Link to the Functions which you mean? This would be very nice and helpfull.

Look at this example:

https://playground.babylonjs.com/#SYQW69#385

It is using the Append function, but it’s working the same with ImportMesh.

1 Like

Thank you :slight_smile: Now it works :slight_smile: