- Home /
Changing the parent of an object in a script
Hi, 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)?
thanks!
Answer by aldonaletto · Nov 15, 2011 at 05:25 PM
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.
Answer by DaveA · Nov 15, 2011 at 05:24 PM
In both cases:
transform.parent = newParent; // where newParent is also a transform
http://unity3d.com/support/documentation/ScriptReference/Transform-parent.html
Your answer
Follow this Question
Related Questions
Update Parent/ Children From Script? 2 Answers
Finding a script on an object 1 Answer
Only In Range 1 Answer
Issue with my Teleport Script 1 Answer
GetComponentInChildren(Renderer).active wont work? 2 Answers