- Home /
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.
Pic2.
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[i] = new Vector3(x * offset, 0, z * offset);
Instantiate(cube, points[i], Quaternion.identity);
}
}
}
}
Answer by arko · Aug 08, 2016 at 02:12 PM
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[i] = new Vector3(x * offset, 0, z * offset);
GameObject go = (GameObject) Instantiate(cube, points[i], Quaternion.identity);
if(x==0 && z==0)
{
boxParent = go;
}
else
{
go.transform.SetParent(boxParent.transform);
}
}
}
}
Answer by StupidlySimple · Aug 08, 2016 at 06:28 AM
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[i] = new Vector3(x * offset, 0, z * offset);
(Instantiate(cube, points[i], Quaternion.identity) as GameObject).transform.parent = cube.transform;
}
}
}
}
There's probably elegant way to do it, but I'm lazy.
Of course it is because you're duplicating cube as child of a cube.
Cube1 start with script will make copy of itself and add it as child so:
Cube1 --- Cube2
The moment Cube2 created, the script get triggered and force it to create another cube, thus
Cube1 --- Cube2 ------- Cube3
And so on.
Lol... anyway, the idea is set all those three cubes as child of the first cube via transform.parent. That way the first cube move in world space, the three will followed.
Answer by saldavonschwartz · Aug 08, 2016 at 08:25 AM
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
.
Your answer
Follow this Question
Related Questions
How do i Instantiate gameObjects in between multiple points? 2 Answers
Creating a GameObject variable without instantiating it? 1 Answer
Instantiated Object Wont save a reference to the prefab, instead sets to itself 1 Answer
What is the best way to instatiate an array of connected GameObjects? 0 Answers
Calling an Audio Source on one game object from a script on another game object..? 1 Answer