- Home /
Children not working with Parent Object properly
Hi guys,
I've come across an error that's got me stumped.
I'm working on some code that will instantiate blocks (using a prefab) into a certain arrangement; a bigger cube. As the cubes are spawned they should become the children of an existing game object created solely for holding this blocks. Then in the FixedUpdate() I perform some basic physics; making the giant cube of individual blocks turn as if they were all one object.
The problem I'm getting, however, is that the blocks are spawned into their correct arrangement, then themselves do nothing. I noticed that while they are not moving, a new block seems to have been created inside of them and is actually working the way I would expect. This is not what I want, though. As mentioned I'd prefer the original blocks move as one.
Here's my code:
using UnityEngine;
using System.Collections;
public class CubeSpawn : MonoBehaviour {
private Transform[] allChildren;
private GameObject parentCube;
public GameObject Cube;
private Vector3 spawnPos = new Vector3(-4.83f,5f,5f);
private int cLineMax = 2; //max cube spawn int
private float distanceBetweenCubes = 1.5f;
// Use this for initialization
void Start () {
//parentCube.AddComponent<Rigidbody>();
for(int i=0; i<cLineMax; i++)
{
for(int j=0; j<cLineMax; j++)
{
for(int k=0; k<cLineMax; k++)
{
parentCube = Instantiate(Cube,
new Vector3 (spawnPos.x + i*distanceBetweenCubes,
spawnPos.y + j*distanceBetweenCubes,
spawnPos.z + k*distanceBetweenCubes),
Quaternion.identity) as GameObject;
//Instantiates the cubes into a parent gameobject
}
}
}
allChildren = parentCube.GetComponentsInChildren<Transform>();
//grabbing every cube in the parent transform
}
// Update is called once per frame
void Update () {
}
void FixedUpdate () {
foreach (Transform child in allChildren) {
child.transform.Rotate(Vector3.right * Time.deltaTime);
}
}
}
If anyone is able to point out where I'm going wrong (likely it's multiple places) I'd REALLY appreciate it; this has me stumped.
Thanks guys.
Answer by DMGregory · May 04, 2014 at 03:56 PM
GameObjects created with Instantiate() sit at the top level of the hierarchy by default.
If you want them parented to another object, do something like this...
GameObject child = Instantiate(prefab, worldspacePosition, worldspaceRotation) as GameObject;
child.transform.parent = parent.transform;
Also, note that in your cube-creation loop you are continuously redefining parentCube. At the end of that loop, the variable parentCube is pointing only at the last-created cube. So GetComponentsInChildren() will only return the transforms inside that cube, not the other cubes you've spawned.
Finally, even if you had the parenting set up the way you intend, child.transform.Rotate() would not make the whole collection of children "turn as if they were all one object." To do that, you would want to rotate their parent. Rotations applied to the children will occur along their own local axes, not the parent's axes.
Hi, thanks for your reply - it was really helpful. I do still have a few questions, though. If it wouldn't be too much trouble just clearing some things up.
Firstly, the child.transform.parent = parent.transform;
- I'm guessing I just leave this in the loop underneath the instantiate instuction? Also will this provide a fix to the constant redefining of parentCube? If not, then my third question would be how can I go about doing this?
Once again, thanks for your time.
Your loop should look something like...
parentCube = new GameObject("parent");
parentCube.transform.position = Vector3.zero; // Or wherever you want the center of the cube to be.
for(i, j, k...) // Abbreviating to fit in a comment.
{
GameObject child = Instantiate(Cube) as GameObject; // Add initial position
child.transform.parent = parentCube.transform;
}
Note that parentCube is assigned exactly once, at the very top. I use a temporary variable to hold each child reference only as long as I need it.
Hi,
I tried the first line in the constructor but I got an error telling me "Unexpected symbol = in class, struct, or interface member declaration", and also the same message about the "parent" part of the line. So, I tried it in the start function and ins$$anonymous$$d got errors telling me "The name parentCube does not exist in the current context". Do you have any idea why this might be?
public GameObject Cube;
private Vector3 spawnPos = new Vector3(-4.83f,5f,5f);
private Vector3 parentCubeCentre = new Vector3(-2.415f,5f,5f);
private int cLine$$anonymous$$ax = 2; //max cube spawn int
private float distanceBetweenCubes = 1.5f;
// Use this for initialization
void Start () {
parentCube = new GameObject("parent");
parentCube.transform.position = parentCubeCentre;
for(int i=0; i<cLine$$anonymous$$ax; i++)
{
for(int j=0; j<cLine$$anonymous$$ax; j++)
{
for(int k=0; k<cLine$$anonymous$$ax; k++)
{
GameObject child = Instantiate (Cube,
//parentCube = Instantiate(Cube,
new Vector3 (spawnPos.x + i*distanceBetweenCubes,
spawnPos.y + j*distanceBetweenCubes,
spawnPos.z + k*distanceBetweenCubes),
Quaternion.identity) as GameObject;
//Instantiates the cubes into a parent gameobject
child.transform.parent = parentCube.transform;
}
}
}
}
Thanks for being so helpful :)
You used to have:
private GameObject parentCube;
in the class body (ie. between "public class CubeSpawn" and "Start") - why did you get rid of it? You can't use a variable that you haven't declared anywhere.
Oops!! $$anonymous$$y bad, I thought the " parentCube = new GameObject("parent");" was a new declaration and therefore the original wasn't needed. I just tried this now and it appears to work!
Thanks so much, you have no idea how helpful this is.
Your answer

Follow this Question
Related Questions
fixing rotation on a specific axis 1 Answer
Impossible to rotate child using parent rotation gizmo 1 Answer
Parenting instantiated particle system 1 Answer
Particle orientation from parent 0 Answers
Detach a child from a parent 1 Answer