Changing the parent of an object in a script

How would I go about doing this? I have an object that is the child of the main camera during an intro sequence that I want to be changed so that it is the child of a different camera that is created at the end of the sequence.

A)How can I change the parent?

B)How can I change it to an object that hasn’t been created in the editor (instantiated)?


The parent is defined in transform.parent. Just assign the new object transform to the object’s parent, like this:

var objPrefab: GameObject; // new object prefab
    var newObject: GameObject = Instantiate(objPrefab, ...); // create the new object
    transform.parent = newObject.transform; // newObject is this object's new daddy!

NOTE: This code is supposed to be attached to the object that will be childed to the new object.

In both cases:

transform.parent = newParent; // where newParent is also a transform