x


GameObjectWithTag Child

Question, how do I find a gameobject with tag which is a child of a gameobject? For some reason gameobjectwithtag doesnt works the same as gameobject.find... Well? Many thanks.

more ▼

asked Jul 11 '12 at 01:21 AM

benk0913 gravatar image

benk0913
100 10 17 23

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

1 answer: sort voted first

Good question and upvoted! I made a sample script that solves the issue hopefully.

#pragma strict
#pragma downcast

var parent : Transform;

function Start () {
    var childObject : GameObject = FindChildGameObjectWithTag(parent, "MyTag");
    if (childObject) Debug.Log(childObject.name+" was found with your tag");
}

function FindChildGameObjectWithTag (p : Transform, tagName : String) : GameObject {
    for (var child : Transform in p) {
        if (child.tag==tagName) {
            return child.gameObject;
        }
    }
    return null;
}
more ▼

answered Jul 11 '12 at 01:58 AM

save gravatar image

save
8.3k 22 31 63

Can you translate it in to C#?

Jul 12 '12 at 04:48 AM benk0913
(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:

x2177
x427
x337
x212
x155

asked: Jul 11 '12 at 01:21 AM

Seen: 339 times

Last Updated: Jul 12 '12 at 04:48 AM