spawn prefabs as children of another game object

i am making an endless runner where the player runs on top of a cylindrical - continuously rotating world. now i need to spawn obstacles (prefabs) on the cylinder and make them(prefabs) have the same motion as the cylinder… im done with the spawning but do not know how to place the spawned prefab as a child of another prefab(the level)so that they move along with the level. please help.

To attach a GameObject as a child of another GameObject you need to set its transform.parent to that other GameObject’s transform. For example if you want to attach a new prefab copy to this GameObject you would write something like this:

GameObject obj = Instantiate(Prefab) as GameObject;
obj.transform.parent = transform;

Update:
Changed your code so it should work. Provided that you want the new brick copy to be attached to current GameObject.

var brick : GameObject;
function Start () {
  var object = Instantiate(brick, Vector3 (0, 0, 10), Quaternion.identity);
  object.transform.parent = transform;
}

Ok so you need to set the parent object after you instantiate the new object as follows.

Gameobject GO = Instantiate(myPrefab);

myPrefab.trasnform.parent = ParentGameObject;
  • myPrefab is the one you want to instantiate