Hello,
I need to find the top parent of an object. So if my collider hits Object then it will return Rock. Rock being the object containing all the other objects.
otherObject/Object/otherObject/Rock
How do i do this? Sorry if that’s hard to follow.
Thank you,
ACMaier
In your formatting i’m reading Rock is actually the bottom child of the tree (which would be unpredictable…)
The top parent seems like an unhandy thing to be looking for at first… what are you actually planning to use it for? It might well be that later on you want that “top parent” divisioned in a parent anyways, ruining the script and possibly whole structure of your game.
I’m having a feeling there’s an easier way to achieve what you’re looking for
childObject.transform.parent.gameObject (is a single step in the right direction)
If you always want to get to the top you will need to use traversal, this will handle multiple steps without having to know how many there are before hand. You could use a recursive function, for example.
Agreed
Transform.root
–Eric
Thank you.
transform.root is what i was looking for. I made this function before you replied:
function HitObject(Hit : GameObject) { //What does the object do when it hits a rock?
if(destroyEffect != null) {
var destroyEffectClone = Instantiate(destroyEffect, Hit.transform.position, Hit.transform.rotation);
SetSpeed(destroyEffectClone);
}
var i = 0;
var trans = Hit.transform;
while (i <= 5){//The counter in not really needed because there will at some point be no more parents.
if(trans.parent != null) {
trans = trans.parent;//
i++;
} else {
i = 6;//End the loop when we find the root
}
}
gameController.GetComponent(PoolManager).Destroy(trans.gameObject);
}