# How do i Instantiate Objects to a Empty GameObject as a Child??

Hello i am having some trouble trying to achieve this in Pic1 where i can move the other cubes along with the base one, but i cant seem to achieve that, here are my results in Pic2. does anyone know how to do this ?

Pic1.
[75652-capture06.png*_|75652]

Pic2.
[75654-capture07.png*_|75654]

Here is my code so you know what i have done.

``````using UnityEngine;
using System.Collections;

public class InstantiateCubes : MonoBehaviour {

public int xSize, zSize;
private Vector3[] points;
public GameObject cube;
public int offset;
void Start () {
points = new Vector3[(xSize + 1) * (zSize + 1)];
for(int i = 0, x = 0; x < xSize; x++)
{
for(int z = 0; z < zSize; z++, i++)
{
points _= new Vector3(x * offset, 0, z * offset);_
``````

Instantiate(cube, points*, Quaternion.identity);*
}
}
* }*
}
_*
_*
@StupidlySimple

You can have the first cube as the parent of the other cubes or have an empty game object as the parent of all the cubes. Lets say you want to use the first cube (instantiated at x=0,z=0) to be the parent. All you have to do when you instantiate the first cube, you save its reference and then when instantiating other cubes set their parent to the first cube. The modified code snapshot would be like this

``````void Start () {
points = new Vector3[(xSize + 1) * (zSize + 1)];

GameObject boxParent; //The parent

for(int i = 0, x = 0; x < xSize; x++)
{
for(int z = 0; z < zSize; z++, i++)
{
points _= new Vector3(x * offset, 0, z * offset);_
``````

GameObject go = (GameObject) Instantiate(cube, points*, Quaternion.identity);*

if(x==0 && z==0)
{
boxParent = go;
}
else
{
go.transform.SetParent(boxParent.transform);
}
}
}
}

public class InstantiateCubes : MonoBehaviour
{
public int xSize, zSize;
private Vector3 points;
public GameObject cube;
public int offset;
void Start()
{
points = new Vector3[(xSize + 1) * (zSize + 1)];
for (int i = 0, x = 0; x < xSize; x++)
{
for (int z = 0; z < zSize; z++, i++)
{
points = new Vector3(x * offset, 0, z * offset);
(Instantiate(cube, points*, Quaternion.identity) as GameObject).transform.parent = cube.transform;*
}
}
}
}

@alexander11

As was mentioned in the other answer, parenting is achieved by means of the transforms of objects.
If you have `GameObject`’s A and B and `B.transform.parent = A.transform` B is a child of A.

From your drawing it is not entirely clear to me if you are trying to make all cubes the children of ‘Base’ while keeping their relative positions or if you want something else. But just in case, this might help you:

You might want to express an object’s transform relative to the frame of reference established by the parent’s transform.

That is, if A is at 0,0,0 and B is at 1,0,0 (1 unit to the right from it’s parent), you probably want things set up in such way that if you then move A to 1,0,0 B ends up at 2,0,0 and not still at 1,0,0. To draw this distinction, the transform component has `postion` and `localPosition`. If you want the child’s position relative to the parent, you should use `localPosition`.