# How to wait asset loading then render the scene?

**URL:** <https://forum.babylonjs.com/t/how-to-wait-asset-loading-then-render-the-scene/22191>\
**Category:** Questions\
**Created:** [July 6, 2021, 10:31am UTC](https://forum.babylonjs.com/t/how-to-wait-asset-loading-then-render-the-scene/22191 "2021-07-06T10:31:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Parmex](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/parmex/32/17196_2.png) [@Parmex](https://forum.babylonjs.com/u/Parmex)\
**Post date:** [July 6, 2021, 10:31am UTC](https://forum.babylonjs.com/t/how-to-wait-asset-loading-then-render-the-scene/22191/1 "2021-07-06T10:31:12Z")

</div>

Hi! I have a problem about asset loading, I would like to load one or more assets, and then create some instances of the asset; I am trying to use AssetManager.  
My code look like this:

```auto

manager=AssetManager
console.log(loading...)
for each asset:
   task=manager.addMeshTask(asset)
   task.onSuccess(){
       save asset in a data structure
       console.log(loaded asset)
   }

manager.load()
console.log(loading done)

generateMesh(...)

```

Now the problem is that I get an error (mesh is undefined) when I call generateMesh because the mesh is not loaded; in fact the log in the console look like this:

_loading…_  
_loading done_  
_ **error!** _  
_loaded asset_

How is possible to first load all the meshes and then start the scene?

I also tried to put that part of code into the init function like this:

```auto
initFunction().then() => {
  *all the loading*

  manager.onFinish= {
     engine.runRenderLoop( scene.render() )
  }

}

```

But I don’t know if this is a correct approach and the problem still persists.

I know it’s possible to use generateMesh inside task.OnSuccess() but I would like to have a different behaviour for each asset and in this way I should put an if for each asset type.

I think is useful to point out that everything works if the asset is generated after some time because the asset is loaded.

Any help is welcome 🙂

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [July 6, 2021, 12:38pm UTC](https://forum.babylonjs.com/t/how-to-wait-asset-loading-then-render-the-scene/22191/2 "2021-07-06T12:38:06Z")

</div>

A repro case in the playground would definitely help. Normally waiting on scene.whenReady should be favored.

---

<div class="post-metadata">

**Author:** ![Parmex](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/parmex/32/17196_2.png) [@Parmex](https://forum.babylonjs.com/u/Parmex)\
**Post date:** [July 15, 2021, 2:35pm UTC](https://forum.babylonjs.com/t/how-to-wait-asset-loading-then-render-the-scene/22191/3 "2021-07-15T14:35:25Z")

</div>

At the end I managed to partially solve the problem, I don’t know if it is good practice but I did like this:

```auto
*createScene()

manager=AssetManager
console.log(loading...)
for each asset:
   task=manager.addMeshTask(asset)
   task.onSuccess(){
       save asset in a data structure
       console.log(loaded asset)
   }

manager.load()
  manager.onFinish= {
    **main()**
  }

```

where the main function contains essentially everything. In this way the project is started only when the manager finished loading.

Again, i don’t know if this is correct, maybe it’s horrible and in the future it will break everything but for now it works, and I hope it could help someone who is looking for something similar  
🙂

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [July 15, 2021, 2:53pm UTC](https://forum.babylonjs.com/t/how-to-wait-asset-loading-then-render-the-scene/22191/4 "2021-07-15T14:53:40Z")

</div>

sounds perfect to me 🙂
