x


Can I limit a .Find to the Parents and Child and not the Scene?

I would like my Script to search for an object in all parents/childs/Sibling and not the scene.

How would I achieve that?

Edit :

To be more precise, I'm looking for an alternative of a GameObject.Find but that would only search objects with a relation with the script (parents, child, siblings), thus it would not mistake and "Find" object in similar objects in the scene.

so "Ball 1" would not look for objects in "Ball 2" and would limit it's search to it's own parent "World".

more ▼

asked Dec 10 '10 at 09:44 PM

Oninji gravatar image

Oninji
332 43 47 50

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

3 answers: sort voted first

"All parents/childs/sibling" is somewhat vague. Perhaps you could rephrase to be more descriptive. Where do you want to start from? Which direction do you want to go? How far do you want to go? When searching down, would you rather go as deep as you can first, searching from one side of the tree to the other or row by row?

Parenting forms a tree.

Searching only this GameObject's parents (going up the tree):

var foo : Transform;
var current : Transform = transform;
while(current.parent) {
    current = current.parent;
    if(current.name == "foo") {
        foo = current;
        break;
    }
}

Searching all immediate children (searching the next row of the tree can be achieved by Transform.Find:

var foo = transform.Find("foo");

To search from the highest parent (the top of the tree), you would need to get this root transform and do the above there like so:

var foo = transform.root.Find("foo");

This is more meaningful if you know the exact path which you would delimit with '/' and grandchildren will be searched.

To search only siblings, you would go up one parent and then check all of its children like so:

var foo : Transform;
if(transform.parent) foo = transform.parent.Find("foo");

It isn't as fast, but to search all children at every level, you would use Transform.GetComponentsInChildren like so:

var foo : Transform;
for(var child : Transform in transform.GetComponentsInChildren(Transform)) {
    if(child.name == "foo") {
        foo = child;
        break;
    }
}

Likewise, to search the entire tree with no knowledge of the relationships, you would do as above with the root:

var foo : Transform;
for(var child : Transform in transform.root.GetComponentsInChildren(Transform)) {
    if(child.name == "foo") {
        foo = child;
        break;
    }
}

Using GetComponentsInChildren uses Depth-First-Search. If you had wanted to do a Breadth-First-Search, you would have to write one like so:

var foo : Transform;
var queue : Array = new Array();
for(var child : Transform in transform) queue.push(child);
while(queue.length != 0) {
    var current = queue.shift();
    if(current.name == "foo") {
        foo = current;
        break;
    }
    for(var child : Transform in current) queue.push(child);
}  
more ▼

answered Dec 10 '10 at 10:07 PM

skovacs1 gravatar image

skovacs1
10k 11 25 91

Like all my other attempts, it returns me Assets/Scripts/Menu/lockScript.js(9,34): BCE0022: Cannot convert 'UnityEngine.Transform' to 'System.Type'.

Dec 10 '10 at 10:37 PM Oninji

Nevermind, forgot a detail. Erf.

Dec 10 '10 at 10:39 PM Oninji

The fifth one worked, toyed with it and could fetch the GameObject from it. Thanks.

Dec 10 '10 at 10:42 PM Oninji
(comments are locked)
10|3000 characters needed characters left

It's already built into the engine. You need to grab all the children from the parent by following along here:

http://unity3d.com/support/documentation/ScriptReference/GameObject.GetComponentsInChildren.html

The code you will want to modify to check each object for a tag. (I'm assuming you want an alternative to FindGameObjectByTag

var transforms : Transform[];
transforms = gameObject.GetComponentsInChildren(Transform);
for (var transform : Transform in transforms) {
    if(transform.gameobject.tag == "target") {
      //Do what you like.
    }
}

Looking at your addendum, I think what you want to do is get the parent of the objects you want to search, then cycle through all children via the GetComponentsInChildren above until you get the GameObject you are looking for.

more ▼

answered Dec 10 '10 at 10:06 PM

Thom Denick gravatar image

Thom Denick
1.9k 13 20 38

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

answered Dec 10 '10 at 11:00 PM

Justin Warner gravatar image

Justin Warner
6.3k 19 27 65

(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:

x5070
x2083
x422
x410
x207

asked: Dec 10 '10 at 09:44 PM

Seen: 1850 times

Last Updated: Dec 10 '10 at 10:35 PM