- Home /
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".
Answer by skovacs1 · Dec 10, 2010 at 10:07 PM
"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);
}
Like all my other attempts, it returns me Assets/Scripts/$$anonymous$$enu/lockScript.js(9,34): BCE0022: Cannot convert 'UnityEngine.Transform' to 'System.Type'.
The fifth one worked, toyed with it and could fetch the GameObject from it. Thanks.
Wish there was a way to find all root objects in the scene. :-P
Given any transform, you can find a root object with transform.root -- so then if you could find all that root's siblings you'd be all set. Unfortunately you can't find siblings except by going through a parent, which for a root node doesn't exist. :-P :-P
@yoyo you can do:
GameObject [] objs = GameObject.FindObjectsOfType<GameObject>();
List<GameObject>list = new List<GameObject>();
foreach(GameObject obj in objs){
if(obj.transform.root == obj.transform){
list.Add(obj);
}
}
return list.ToArray();
This will return all active top objects in your scene.
Answer by Thom Denick · Dec 10, 2010 at 10:06 PM
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.
Answer by Justin Warner · Dec 10, 2010 at 11:00 PM
See Accessing Other Game Objects in the Unity 4.1.2 documentation.
Check number 2 please.
Should be good =).
Your answer
Follow this Question
Related Questions
Make a simple tree 1 Answer
how do I get reference to a parent or a child? 2 Answers
"Center On Children" programmatically 1 Answer
How to destroy a parent object when child object is collided 1 Answer
change pivot of parent game object 1 Answer