• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
50
Question by Lipis · Mar 01, 2010 at 03:06 AM · javascriptgameobjectparentchild

How can I get a parent GameObject of gameObject using Javascript?

I have grouped few GameObject into one GameObject. Is it possible to get the parent GameObject if you know the child GameObject using Javascript?

Something similar to the transform.parent.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

6 Replies

· Add your reply
  • Sort: 
avatar image
185
Best Answer

Answer by Eric5h5 · Mar 01, 2010 at 03:25 AM

childObject.transform.parent.gameObject

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Lipis · Mar 01, 2010 at 03:51 AM 0
Share

It was easier than I thought :)

avatar image alexnode · Apr 06, 2010 at 03:19 PM 1
Share

Thanks, Eric I was searching for that for ages!

avatar image sona.viswam · Feb 15, 2013 at 03:51 AM 0
Share

Similar, how i get a specific child object without using for loop?

avatar image edmundo096 · Aug 27, 2013 at 07:56 PM 1
Share

@sona.viswam Probably if you know the child name with transform.FindChild(childString) Or if you know the index of the child with transform.GetChild(childIndex)

avatar image
13

Answer by RapetorJesus · Nov 15, 2010 at 09:00 AM

Thanks hahaha finally this power is miiiiine!

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image obsidian1269 · Jun 27, 2011 at 03:16 PM 2
Share

And what a glorious power it is!

I too share your enthusiasm. The rest of you can take your downvotes and pop-shuvit late backfoot kickflip.

avatar image
4

Answer by alok.kr.029.hotmail · Aug 06, 2014 at 05:06 AM

Gameobject obj = this.transform.parent;

with the help of this code you can get the parent of the current gameobject to which script is attached

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image David_Brooks · Jun 20, 2016 at 03:42 AM 3
Share

obj = this.transform.parent returns the transform of the parent.

To get the game object you can either just add .gameObject to "this.transform.parent" as per the answer above or refer to "obj.gameObject" elsewhere in your script to refer to the parent game object/define a new GameObject variable as obj.gameObject.

avatar image
3

Answer by CrowaL · Dec 31, 2016 at 05:37 AM

Perfect. And for those that have multiple sub-children, just duplicate transform.parent.gameobject

In this example, there's 2 subchildren. So to acquire the top parent I had to duplicate.

parent (gameobject) -> child (gameobject) ---> child (gameobject)

 gameObject.transform.parent.gameObject.transform.parent.gameObject

Sharing is Caring

https://twitter.com/IndieNendoroid

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Norair1997 · May 11, 2018 at 03:11 PM 3
Share
 // ins$$anonymous$$d of this
 gameObject.transform.parent.gameObject.transform.parent.gameObject
 
 // use this
 transform.parent.parent.gameObject

because "parent" returns you a Transform, and from this transform you want its parent... and you don't need to refer to gameObject at the beginning. because this.transform and this.gameObject are refered to this

 gameObject.transform = transform
 transform.gameObject = GameObject
avatar image
-2

Answer by munaeem · Nov 10, 2012 at 09:59 PM

function OnTriggerEnter (other :Collider)
{ other.transform.parent = gameObject.transform; } function OnTriggerExit (other : Collider) { other.transform.parent = null; }

Here you go Full script. just attach this script to your emptyObject with triggered collider on it. but make sure the empty object is a child of the cube/platform who has the animation on it and this script goes to that child. and done. the character looks squeezed but if you have any solution please provide me one and i am working on it aswell.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Make a simple tree 1 Answer

Making cube a child of an object. 1 Answer

Instantiate Terrain Object as child of Empty Game Object 1 Answer

How to destroy a parent object when child object is collided 1 Answer

Creating new Transform from existing objects Transform to Instantiate object 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges