Search code examples
c#unity-game-enginepuzzletetris

UNITY.. C#. How to make 3 gameObjects as one without losing each of gameObjects positioning


I am making a puzzle using drag & drop in unity. The puzzle piece is like Tetris-piece, each puzzle piece is composed of a group of cubes. the puzzle piece needs to be dragged and then dropped into the slots of missing cubes of the structure(e.g. rectangle). The code is nicely done, but I am struggling in making objects.

Here's the screenshot of my sample game.

Heres the scenario

The problem is each structure is composed of a group of cubes. when I drag a single cube it perfectly goes to the slot where I dropped it and that slot will be occupied. But when I drop a piece which is composed of two cubes. it only occupies one slot,because, that piece, although has two cubes, is counted as one object, because it's stored in a parent object , another problem is the object's anchor is placed at the center so it will not fit each block because that anchor will be the one that will find the nearest slot where it will be dropped. So, My question is how do I make an object(parent) with many cubes(children) without it making as one? or without losing each objects positioning?


Solution

  • You can simply drag game objects over others in the hierarchy window to create children which can then be relatively positioned within the parent.

    Perhaps look into Prefabs (https://unity3d.com/learn/tutorials/topics/interface-essentials/prefabs-concept-usage) they will allow you to group game objects and position them as a group, position each sub game item individually using a local position (relative to the parent) which is essentially the same functionality but are grouped outside of the hierarchy window and can be dropped into a scene as a group or instantiated through script