|
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".
(comments are locked)
|
|
"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):
Searching all immediate children (searching the next row of the tree can be achieved by Transform.Find:
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:
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:
It isn't as fast, but to search all children at every level, you would use Transform.GetComponentsInChildren like so:
Likewise, to search the entire tree with no knowledge of the relationships, you would do as above with the root:
Using GetComponentsInChildren uses Depth-First-Search. If you had wanted to do a Breadth-First-Search, you would have to write one like so: 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)
|
|
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
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.
(comments are locked)
|
|
http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html Check number 2 please. Should be good =).
(comments are locked)
|
