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 () {


		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.

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.