# Less drag and more drop with a button

**URL:** <https://forum.babylonjs.com/t/less-drag-and-more-drop-with-a-button/2026>\
**Category:** Questions\
**Created:** [March 20, 2019, 8:04pm UTC](https://forum.babylonjs.com/t/less-drag-and-more-drop-with-a-button/2026 "2019-03-20T20:04:05Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Modos](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/modos/32/317_2.png) [@Modos](https://forum.babylonjs.com/u/Modos)\
**Post date:** [March 20, 2019, 8:04pm UTC](https://forum.babylonjs.com/t/less-drag-and-more-drop-with-a-button/2026/1 "2019-03-20T20:04:05Z")

</div>

Hi again.  
Prepare to give me a promotion for creating 100 topics 🙂

[https://playground.babylonjs.com/#279FW9#44](https://playground.babylonjs.com/#279FW9#44)

I want to create a mesh with a button, attache it to the cursor and drop it, when click again﻿.  
It works fine, except, when I create another mesh, the position of the old mesh will also be set again, so I have two or more meshes on the same position and it seems, that it is only one mesh.  
How can I manage, to let the first mesh be fixed on its position, when creating a new mesh?

Also, is it better to use the _createInstance_ function, instead of creating a mesh with _new Mesh_ on every function call, like I did in the playground?

---

<div class="post-metadata">

**Author:** ![Galen](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/galen/32/539_2.png) [@Galen](https://forum.babylonjs.com/u/Galen)\
**Post date:** [March 21, 2019, 12:59am UTC](https://forum.babylonjs.com/t/less-drag-and-more-drop-with-a-button/2026/2 "2019-03-21T00:59:37Z")

</div>

OK, not a problem. You can’t dispose of your event listeners on every click. How’s this:

[https://playground.babylonjs.com/#279FW9#54](https://playground.babylonjs.com/#279FW9#54)

Galen

---

<div class="post-metadata">

**Author:** ![Galen](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/galen/32/539_2.png) [@Galen](https://forum.babylonjs.com/u/Galen)\
**Post date:** [March 21, 2019, 1:17am UTC](https://forum.babylonjs.com/t/less-drag-and-more-drop-with-a-button/2026/3 "2019-03-21T01:17:52Z")

</div>

If you want to add multiple boxes, give them unique IDs

---

<div class="post-metadata">

**Author:** ![lcarlos](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/lcarlos/32/1353_2.png) [@lcarlos](https://forum.babylonjs.com/u/lcarlos)\
**Post date:** [March 21, 2019, 3:14am UTC](https://forum.babylonjs.com/t/less-drag-and-more-drop-with-a-button/2026/4 "2019-03-21T03:14:00Z")

</div>

Solution was to create a variable for increment and an empty array.  
look here \>\> [https://playground.babylonjs.com/#ZVBH8D#2](https://playground.babylonjs.com/#ZVBH8D#2)

> 😁

---

<div class="post-metadata">

**Author:** ![JohnK](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/johnk/32/26764_2.png) [@JohnK](https://forum.babylonjs.com/u/JohnK)\
**Post date:** [March 21, 2019, 8:46am UTC](https://forum.babylonjs.com/t/less-drag-and-more-drop-with-a-button/2026/5 "2019-03-21T08:46:36Z")

</div>

You can use instances [https://playground.babylonjs.com/#279FW9#55](https://playground.babylonjs.com/#279FW9#55) however all instances will have the same material. You could replace ‘createInstance’ with ‘clone’ if you want to have different materials.

---

<div class="post-metadata">

**Author:** ![Modos](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/modos/32/317_2.png) [@Modos](https://forum.babylonjs.com/u/Modos)\
**Post date:** [March 22, 2019, 6:24pm UTC](https://forum.babylonjs.com/t/less-drag-and-more-drop-with-a-button/2026/6 "2019-03-22T18:24:07Z")

</div>

Well, alot of responses. Thank you all.

@Galen

> [@Galen](#):
>
> You can’t dispose of your event listeners on every click. How’s this:
> 
> [https://playground.babylonjs.com/#279FW9#54](https://playground.babylonjs.com/#279FW9#54)

Good to know I can delete unnecessary code. I just copied this part from another playground about drag and drop and didn’t even try to let this part away.

> [@Galen](#):
>
> If you want to add multiple boxes, give them unique IDs

I already tried that! At least if it means giving them unique names.  
And that is where we come to:

@JohnK

> [@JohnK](#):
>
> [https://playground.babylonjs.com/#279FW9#55](https://playground.babylonjs.com/#279FW9#55)

You made it work by giving them unique names. I also did that, but I still had the same problem: all created meshes moved to the same pointed spot.  
I tried to adapt your playground to my code.  
**[https://playground.babylonjs.com/#279FW9#62](https://playground.babylonjs.com/#279FW9#62)**

My code:

 ![MyCode](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/2X/1/121dd3b15b6dc9e92c8ed234566b0525aaef1d8c.png)

_createName_ is a function that returns a string with ‘barrack’ in it. It is unique, I tested it in the debuglayer. I don’t see any important difference in my code and the playground…but the playground is working like I want it to.

@lcarlos  
Same with your code. When I adapt your playground to my code on desktop, it is working fine. But tbh I rather want to refere the mesh with a name. I want to have as less global variables as possible 😅

Both playgrounds are working fine, so that means my code is the problem.  
Is this screenshot enough for anyone to see the error in my code? I’d be glad.

---

<div class="post-metadata">

**Author:** ![JohnK](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/johnk/32/26764_2.png) [@JohnK](https://forum.babylonjs.com/u/JohnK)\
**Post date:** [March 22, 2019, 6:46pm UTC](https://forum.babylonjs.com/t/less-drag-and-more-drop-with-a-button/2026/7 "2019-03-22T18:46:55Z")

</div>

Sorry not to have time to do more until tomorrow or the day after. In the mean time note that onPointerMove, onPointerUp, canavs.addListener are methods of the scene not of a box (barrack) so should not be inside the ‘creatingBoxes()’ or ‘createBarrack’ functions.

---

<div class="post-metadata">

**Author:** ![JohnK](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/johnk/32/26764_2.png) [@JohnK](https://forum.babylonjs.com/u/JohnK)\
**Post date:** [March 23, 2019, 12:21pm UTC](https://forum.babylonjs.com/t/less-drag-and-more-drop-with-a-button/2026/8 "2019-03-23T12:21:05Z")

</div>

Here is a version that maybe closer to what you want [https://playground.babylonjs.com/#279FW9#63](https://playground.babylonjs.com/#279FW9#63). Difficult to know without knowing exactly what you are trying to achieve.

In the case of global variables, imho, it is more to do with where they belong to the scene or the box for example rather than doing away with them because ‘they are bad’. To do away with them completely you need to write fully Oops code.

---

<div class="post-metadata">

**Author:** ![lcarlos](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/lcarlos/32/1353_2.png) [@lcarlos](https://forum.babylonjs.com/u/lcarlos)\
**Post date:** [March 23, 2019, 9:13pm UTC](https://forum.babylonjs.com/t/less-drag-and-more-drop-with-a-button/2026/9 "2019-03-23T21:13:35Z")

</div>

> > > Same with your code. When I adapt your playground to my code on desktop, it is working fine. But tbh I rather want to refere the mesh with a name. I want to have as less global variables as possible ! \<\<\<\<,

About this! You can get the name of each object within the array that you created and all were created with unique names.

about your code! What do you want to do with this part of the player?

---

<div class="post-metadata">

**Author:** ![Modos](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/modos/32/317_2.png) [@Modos](https://forum.babylonjs.com/u/Modos)\
**Post date:** [March 24, 2019, 7:59pm UTC](https://forum.babylonjs.com/t/less-drag-and-more-drop-with-a-button/2026/10 "2019-03-24T19:59:02Z")

</div>

> [@JohnK](#):
>
> Difficult to know without knowing exactly what you are trying to achieve.

> [@lcarlos](#):
>
> about your code! What do you want to do with this part of the player?

The goal is to create and place a building with a button, like in RTS games like Age of Empires, Starcraft, Warcraft etc.

> [@JohnK](#):
>
> Here is a version that maybe closer to what you want [https://playground.babylonjs.com/#279FW9#63](https://playground.babylonjs.com/#279FW9#63).

No need to do more stuff, the earlier playgrounds of you guys were already my solution. Implementing them in my code just didn’t work out (I’m coming to that).

> [@JohnK](#):
>
> In the mean time note that onPointerMove, onPointerUp, canavs.addListener are methods of the scene not of a box (barrack) so should not be inside the ‘creatingBoxes()’ or ‘createBarrack’ functions.

Usually I know this. I was still doing it, because I was refering to _var name_, which I initialized in the function.  
I figured out that this was the problem all along.  
Although I created a unique name with _var name = createName()_,  
for some reason all names at the same time were used, when I called

```
function onPointerMove(evt) {
        if(!clicked) { return; }
        scene.getMeshByName(name).position.x = scene.pick(scene.pointerX, scene.pointerY, null, null).pickedPoint.x;
        scene.getMeshByName(name).position.z = scene.pick(scene.pointerX, scene.pointerY, null, null).pickedPoint.z;
    }

```

so, all meshes were positioned to the same spot.  
When I use the counter in the name, everything is working fine.

> **Just to show what I mean, how it works now**
>
> ![myWorkingCode](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/2X/3/319c1ecff802c24f9870c16c8367ab05b7ed8297.png)

I’m still curious:  
(Without the counter) Even when I refere the mesh in the _onPointerMove_ function with the variables name with which I created a mesh, like

```
var mesh = new BABYLON.MeshBuilder.CreateMesh(...)
onPointerMove {
    mesh.position.x = scene.pick(X)
    mesh.position.z = .position.x = scene.pick(Z)
}

```

all meshes created with this function will be moved.  
Example: [https://playground.babylonjs.com/#279FW9#65](https://playground.babylonjs.com/#279FW9#65)  
Basically the same as before. Just to illustrate what I mean.  
When I create a new mesh with _var mesh = createMesh()_, shouldn’t just this currently created mesh be moved, instead of all meshes? I’m trying to understand what is causing this. The main problem is solved.

What I want to achieve with that information, is a way to use my _var name = createName()_ function without a meshCounter.  
Maybe when the _onPointerMove_ function is outside, but called inside my function and is given the mesh to move as parameter? I didn’t got that to work, but I believe this should work somehow.  
I have similar functions like this, the only difference is, that created meshes have a fixed position and it all works fine. They have a unique name and they move independently.

I hope I made my question clear. If not, no reply wouldn’t be that bad, because I have the solution of my main problem 🙃
