- Home /
How can I find a unique sibling/parent/child in a specific hierarchy and not from all the scene?
I am running to this problem again and again... of confusing the unique and non unique ways for finding gameObjects.
If i have a child (let's say) trigger, i can access its parent by doing
yourgameObject.transform.parent.gameObject
This parent is a unique game object and if I clone twenty of those prefabs whenever I hit them I will get the right parent from the right corresponding hierarchy.
With
gameObject.FindWithTag "xxx"
I get a random first object that matches the criteria of my search. But If you duplicate ten prefabs with this code they won't work because it finds a random one (or the first one??). It is good for unique tags like "player" in a singleplayer game.
gameObject.Find "xxx/xxx/xxx"
you find the unique children.
Please let me know if I am right until here.
Is there any way to find a unique sibling(grandcousin!)? do I have to use root and then find ?
///////////////////// Edit after answer
after Ducks suggestion I did a code that looks like that to point to children gameObjects by name. I am still missing something I think but I works.
var allChildren = currentRoot.GetComponentsInChildren(Transform); for (var child : Transform in allChildren) { if(child.gameObject.name == "MyObject") { currentRemoteChild = child.gameObject ;
}
}
Answer by duck · Apr 06, 2010 at 01:11 PM
If you're cloning those twenty prefabs at runtime (using Instantiate) then you should put the references returned by each Instantiate call into an array.
You then have references to each and every instance of the cloned object.
If your clones already exist, you can make them all children of a particular object, and then you can iterate through those child objects, as shown in this answer:
thanks Duck, no instantiate just placing moving platforms, springs etc..I want to parent something to a child of a sibling. I will try with get components in children to see where it goes.
Your answer
Follow this Question
Related Questions
Make a simple tree 1 Answer
Finding child problems.. 1 Answer
How do I find a child object in a hierarchy of children? 1 Answer
Removing specific child objects 1 Answer