x


Traverse up the hierarchy to find first parent with specific tag

I have a child object that needs to find the first parent object with a certain tag.

Parent Object ("Foo tag") \ Child Object \ Child Object | Child Object | Child Object \ Child Object (needs to traverse up the tree until it finds the "Foo tag"

I can't just do a find for the foo tag because I have multiple objects with that tag - I need to find the one that is a parent of this child.

more ▼

asked Sep 29 '10 at 03:23 PM

Dan 9 gravatar image

Dan 9
22 2 2 4

I could loop through each parent until I find the tag. parent = transform.parent while (parent != null) { if (parent.tag == "Foo tag")... }

Sep 29 '10 at 03:25 PM Dan 9

That's exactly how you'll need to do it

Sep 29 '10 at 03:39 PM Mike 3
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

See comments above for answer. It is working great.

more ▼

answered Sep 29 '10 at 04:03 PM

Dan 9 gravatar image

Dan 9
22 2 2 4

(comments are locked)
10|3000 characters needed characters left

Imagine a form with three uls (as sections). Each section has several 'li checkbox /li' and the last one says Check/Uncheck All. Here is the code, which will change all checkboxes in same ul.

$(document).ready(function(){
  $('.check-all').change( function(){
    $(this).closest("ul").find('input[type=checkbox]').attr('checked', $(this).attr('checked') || false );
  });
});
more ▼

answered Sep 16 '12 at 05:45 PM

NiMareQ gravatar image

NiMareQ
1 1

(comments are locked)
10|3000 characters needed characters left

I hope this helps someone -- and please correct me where I'm wrong!

// FIND PARENT WITH TAG
function findParentWithTag(tagToFind:String) {
    return findParentWithTag(tagToFind, this.gameObject);
}
function findParentWithTag(tagToFind:String, startingObject:GameObject) {
    var parent = startingObject.transform.parent;
    while (parent != null) { 
       if (parent.tag == tagToFind) {
         return parent.gameObject as GameObject;
       }
       parent = parent.transform.parent;
    }
    return null;
}
more ▼

answered Apr 07 '12 at 06:06 PM

bladnman gravatar image

bladnman
16

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x148
x68
x5

asked: Sep 29 '10 at 03:23 PM

Seen: 1988 times

Last Updated: Sep 16 '12 at 05:45 PM