Going up the Hierarchy

Hi All,

I need to search up though my Hierarchy to see if a parent object has a certain tag. My starting point can vary, So currently I have this.

var oneParent =currentSelection.transform.parent;
var twoParent =currentSelection.transform.parent.parent;

Is there a better way to do this, I want a loop really that just keeps on searching up the hierarchy then tells me how many levels up it had to go to get there…

Cheers,
Rusty

This is C# so you’ll have to convert.

Transform currentTarget = transform.parent;
while ((currentTarget!=null)&&(!currentTarget.CompareTag("whatever"))) {
  currentTarget = currentTarget.parent;
}
if (currentTarget == null) {
 //notfound
} else {
 //found
}

Great, Thanks for your help, I try and implement this now!