x


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 ;

                    }

        }
more ▼

asked Apr 06 '10 at 12:53 PM

alexnode gravatar image

alexnode
984 27 32 49

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

1 answer: sort voted first

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:

How can I access the children of a Transform?

more ▼

answered Apr 06 '10 at 01:11 PM

duck gravatar image

duck ♦♦
41k 92 148 415

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.

Apr 06 '10 at 01:34 PM alexnode
(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:

x421
x410
x293
x207
x148

asked: Apr 06 '10 at 12:53 PM

Seen: 3887 times

Last Updated: Apr 07 '10 at 09:24 AM