# How to load object and clone it all over the scene

**URL:** <https://forum.babylonjs.com/t/how-to-load-object-and-clone-it-all-over-the-scene/22376>\
**Category:** Questions\
**Created:** [July 14, 2021, 5:21pm UTC](https://forum.babylonjs.com/t/how-to-load-object-and-clone-it-all-over-the-scene/22376 "2021-07-14T17:21:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![r00xus](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/r00xus/32/17310_2.png) [@r00xus](https://forum.babylonjs.com/u/r00xus)\
**Post date:** [July 14, 2021, 5:21pm UTC](https://forum.babylonjs.com/t/how-to-load-object-and-clone-it-all-over-the-scene/22376/1 "2021-07-14T17:21:57Z")

</div>

Hello everyone!

I need to load an object “prototype” meshs and clone them dynamicly all over the scene according to a certain algorithm.  
Let’s imagine that one of them is the cars that I want to line up 20.  
Here’s what I get

```auto
        var canvas = document.getElementById('canvas');
        engine = new BABYLON.Engine(canvas, true);
        scene = _createScene();
        assetsManager = new BABYLON.AssetsManager(scene);
        // Load car model, lets's call it "prototype"
        var meshTask = assetsManager.addMeshTask('loadCar', '', '/Content/', 'car.glb');   
        var carTmp;
        meshTask.onSuccess = function (task) {
            carTmp = task.loadedMeshes[0];
        }
        assetsManager.onFinish = function () {
            // Arrange the cars on the scene
            for (var i = -10; i < 10; i++) {
                var carNew = carTmp.clone("car" + i);
                carNew.position = new BABYLON.Vector3(0, 0, i * 6);
            }

            // Delete carTmp...?

            engine.runRenderLoop(function () {
                scene.render();
            });
        };
        assetsManager.load();

```

This is certainly not optimal ☹  
As far as I understand, I shoild use the AssetContainer some way.  
What are the best practics for this?

---

<div class="post-metadata">

**Author:** ![PirateJC](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/piratejc/32/1003_2.png) [@PirateJC](https://forum.babylonjs.com/u/PirateJC)\
**Post date:** [July 14, 2021, 5:27pm UTC](https://forum.babylonjs.com/t/how-to-load-object-and-clone-it-all-over-the-scene/22376/2 "2021-07-14T17:27:06Z")

</div>

Hey @r00xus

There are several different ways to go about “cloning” a mesh to use across your scene.

Here are 3 different techniques you can explore:

1. [Clones | Babylon.js Documentation](https://doc.babylonjs.com/divingDeeper/mesh/copies/clones)

2. [Fun with Legos Part 1: Thin Instances - YouTube](https://youtu.be/0DaSvI_n7Us)

3. [Interactive Hex Tiles: Part 1 - Using Asset Containers - YouTube](https://youtu.be/fZajDLsLJKg)

---

<div class="post-metadata">

**Author:** ![r00xus](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/r00xus/32/17310_2.png) [@r00xus](https://forum.babylonjs.com/u/r00xus)\
**Post date:** [July 17, 2021, 7:30am UTC](https://forum.babylonjs.com/t/how-to-load-object-and-clone-it-all-over-the-scene/22376/4 "2021-07-17T07:30:07Z")

</div>

Hi @PirateJC!

Your YouTube videos are awesome and inspire to learn the engine!

Let me explain what I am trying to do in more detail. I made a prototype of the application using the playground

[https://playground.babylonjs.com/#UEZMTP#25](https://playground.babylonjs.com/#UEZMTP#25)

There are such assets as a car and a track that the application loads so that later using them as templates to build a scheme of railway tracks in accordance with the JSON file of each scheme.

I put the car and track templates in the “templates” assets container. During the construction of the schema itself, I also put clones of templates in the “schema” assets container, so that it is more convenient to remove them when loading another schema using `BABYLON.AssetContainer.dispose()` method.

Here’s what goes wrong.  
After loading all the templates, I call the method `BABYLON.AssetContainer.removeAllFromScene` in order not to show them on stage at all. And yes. They don’t really show up in the scene inspector window.  
But they stay in camera viewport!

Please tell me what I’m doing wrong ☹

---

<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 19, 2021, 11:18am UTC](https://forum.babylonjs.com/t/how-to-load-object-and-clone-it-all-over-the-scene/22376/5 "2021-07-19T11:18:12Z")

</div>

@Evgeni_Popov might be able to have a look as I agree it sounds weird they are staying in.

---

<div class="post-metadata">

**Author:** ![r00xus](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/r00xus/32/17310_2.png) [@r00xus](https://forum.babylonjs.com/u/r00xus)\
**Post date:** [July 19, 2021, 11:25am UTC](https://forum.babylonjs.com/t/how-to-load-object-and-clone-it-all-over-the-scene/22376/6 "2021-07-19T11:25:48Z")

</div>

Hello again!

It looks like problems arise only if you use \*.glb files.  
As soon as I changed the file format of the models to \*.babylon everything worked as expected

[https://playground.babylonjs.com/#UEZMTP#26](https://playground.babylonjs.com/#UEZMTP#26)

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [July 19, 2021, 11:33am UTC](https://forum.babylonjs.com/t/how-to-load-object-and-clone-it-all-over-the-scene/22376/7 "2021-07-19T11:33:14Z")

</div>

Your problem comes from:

```javascript
var mesh = task.loadedMeshes[0];
templates.meshes.push(mesh);

```

There are multiple meshes in your .glb files, so you must push them all:

```javascript
templates.meshes.push(...task.loadedMeshes);

```

It works with the .babylon file because each file has only a single mesh.

---

<div class="post-metadata">

**Author:** ![r00xus](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/r00xus/32/17310_2.png) [@r00xus](https://forum.babylonjs.com/u/r00xus)\
**Post date:** [July 19, 2021, 11:48am UTC](https://forum.babylonjs.com/t/how-to-load-object-and-clone-it-all-over-the-scene/22376/8 "2021-07-19T11:48:28Z")

</div>

@Evgeni_Popov Thank you very much for your reply!

And the last question: is my approach optimal or are there some better solutions?

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [July 19, 2021, 1:14pm UTC](https://forum.babylonjs.com/t/how-to-load-object-and-clone-it-all-over-the-scene/22376/9 "2021-07-19T13:14:36Z")

</div>

That seems fine to me!
