Merge Objects In 2D Unity Game | Create A New Object Dragging One Object to Another Combining Them @AlexanderZotov
Merge Objects In 2D Unity Game | Create A New Object Dragging One Object to Another Combining Them  @AlexanderZotov
Uploaded October 2020 | Updated September 2026, 3 weeks ago
How to merge two objects to get a new one? That is what this video is about.

twitch.tv/zzotoff

Here what this video is about.
How to merge objects to create a new one? Let me show a very simple way.
Here I have an empty project. Let’s add some gameobjects. Those will be an acorn, an oak, and a Rocket of course. These gameobject gonna need box colliders to detect mouse click. And also to detect collisions between each other they will need a rigidbody 2d. Set gravity scale to 0 or they will fall down. Ok. One simple script helps us to make this all work.

OnMouseDown is invoked when you press left mouse button over objects collider. That’s why we need those colliders. Without collider it will not work. So when it happens I kind of declare that I hold mouse button down ‘cause I'm gonna need some checking when I release it. Also here I calculate some offset between mouse position and center of game object I’m going to drag. Without this offset my gameobject will jump upto mouse pointer when I start to drag it.

OnmouseDrag is invoked when I drag an object with mouse holding down left mouse button. Everything is pretty simple here. I get Mouse position and set objects position to it using that offset.

OnMouseUp is invoked when mousebutton is released. When it happens I tell the script that button is released and this condition is met. The main part of the magic happens in ontriggerstay method which is being invoked while gameobjects collider is overlapping another collider. So when it’s happening I have to get names of those overlapping gameobjects to be able to perform merge action. Or not to perform. So I get names here. Underscore divider which I add to objects names a bit later will help me to get the part of the name I need. First part in this case which will be Acorn, Oak or Rocket.
Ok. When names are obtained I check if a button is released and names are equal to what I want them to, I instantiate a new Oak or rocket depending on certain conditions. Prefabs will be loaded from the resources folder which I will create in a moment. That’s the script.

I D&D this script to Acorn, Oak and Rocket. To be able to instantiate gameobject I need a prefab of it. And this prefab should be put into resources folder. So I create resources folder in assets folder. Be careful with resources folders name. Just one typo will ruin the whole game. Once the folder is created I D&D my gameobject into it turning them into prefabs. Now I have to rename each prefab so the script could get the part of its name and load its prefab correctly. Adding underscore and object word will do the job. Nice. I can delete these gameobjects from hierarchy. Now I add an Acorn. duplicate it three times. Spread them around the scene a bit. And now I can hit play to see how it works. I can drag each object. And when I overlap them and release a b…. wtf… Ah. I forgot to set colliders to be triggers. Select my prefabs and check in isTrigger option. Now I can hit play and see how it works. I can drag it and if I overlap them and release a button a new gameobject is created. Beautiful. Hope you like this tutorial. Thank you for watching and see you next time.
Merge Objects In 2D Unity Game | Create A New Object Dragging One Object to Another Combining ThemCreate Enemy Run Animation For Among Us 2D Shooter Game Clone Made With Unity SoftwareTutorial How To Move & Control 3D Object With UI Buttons Up Down Left And Right In Mobile Unity GameMove Game Object With Finger Swipe Left And Right In Android Mobile Game Made With Unity SoftwareHow To Create Simple 2D Sniper Scope With Blurred Background And Focused Eyepiece | Unity TutorialSecret Password To Continue Playing A Level You Want Feature For Your Game Made With Unity SoftwareHow To Use Unity UI Button OnClick Function In Your Game | Turn Game Object On And Off TutorialHow to make simple escape puzzle game for Android with Unity software | Color lock featureUnity Tutorial How To Add Coin Pick Up And Coins Counter Features To Android Jetpack Clone GameHow To Create Dynamite Sparkling Fuse Animation With Line Renderer And Particle System In Unity GameUnity 2D Animation Tutorial - Idle Run Jump Animations And Transitions Between Them Using C# ScriptUnity Tutorial How To Create Add Fuel Pick Up Feature In Android Jetpack Clone Game
Alexander Zotov |

Merge Objects In 2D Unity Game | Create A New Object Dragging One Object to Another Combining Them

SHARE TO X SHARE TO REDDIT SHARE TO FACEBOOK WALLPAPER