Is there a way to get the parent of a GameObject in a script?
Answer by 3dDude
Nov 10, 2010 at 10:23 PM
there is 2 ways
1# use transform.parent for the direct parent above
2# use transform.root witch us the very most top parent of the tree
hope this helps!
note: i am a bit rusty on scripting to this might not be right...
This gets you the transform of the parent, but not the parent itself.
@Crissa you can switch back and forth between GameObject and Transfrom by using .gameObject and .transform as appropriate.
In this case use
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
If you are a new user, check out our FAQ for more information.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
For troubleshooting common problems with Unity 5.x Editor (including Win 10).
To help users post good questions and use the site effectively we have posted a user guide. Please check it out.
Answers and Comments
No one has followed this question yet.
Why is there no gameObject.parent?
Best way to animate a guy putting on a hat
Renaming Child Objects during Instantiate?
Does renderer.bounds of Parent GameObject include all children meshes that are rendered?
Attaching a game object as a child during runtime?