- Home /
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
.
Answer by Eric5h5 · Mar 01, 2010 at 03:25 AM
childObject.transform.parent.gameObject
Similar, how i get a specific child object without using for loop?
@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)
Answer by RapetorJesus · Nov 15, 2010 at 09:00 AM
Thanks hahaha finally this power is miiiiine!
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.
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
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.
Answer by KarlKarl2000 · 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
// 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
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220612090925im_/https://answers.unity.com/themes/thub/images/avi.jpg)