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.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
No one has followed this question yet.
In a CustomEditor, how to find the target's parent?
Accessing a GameObject parent Object
Find position of the GameObject this script is attached to.
Using parent tag on whole gameObject
Reparenting game objects is slow