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 user guide.
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).
Answers and Comments
No one has followed this question yet.
C# Check if parent's script's GameObject variable equals GameObject
How to remove the GameObject having the Object
Make a simple tree
Reparenting game objects is slow
How can I access a Collider's GameObject in script?