- Home /
How to transform an empty GameObject that has gameObject children?
I have created an empty gameObject and added child gameObjects from primitives which I reference via a List<>
public class ObjectManager : MonoBehaviour { private List<GameObject> _cubes;
Primitive generation code snippet:-
for(int i = 0; i < cubesPerRow; i++)
{
cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.position = new Vector3(nextX,nextY,0);
nextX = (cube.transform.localPosition.x + cube.transform.localScale.x) + cubeSpacing;
_cubes.Add(cube);
}
This works fine but now I wish to rotate the parent (objectManager) and have all the child cubes affected by this. To reiterate, I want to apply a single transform and have all of the objects move based on their parents transform.
When I apply a single transform to the parent via Update() none of the children move at all, however I can loop through the _cubes collection and apply transforms to them (which is not the desired behavior).
I am used to Flash's parent / child relationships but new to Unity and 3d so hopefully someone can explain the best way to work with Child objects to me going forward.
Thank you.
[EDIT: Here is my Update function]
void Update()
{
float amtToMove = 60.0f * Time.deltaTime;
transform.Rotate(amtToMove, amtToMove, amtToMove * 2);
}
I have also tried with gameObject.transform to no avail.
What code are you're using to rotate the parent at present? (that causes no movement of the children)
I have added the Update code to my original post. $$anonymous$$any thanks.
Answer by Novodantis 1 · Nov 15, 2010 at 11:46 PM
Ah, I think you need this, on the line after creating the cube primitive:
cube.transform.parent = transform;
Although you are holding the list inside this entity, created objects are not part of its hierarchy by default.
[EDITED, sorry, its a property of transform not gameobject]