x


Find a child with a name, how to??

Hello there! I just wanna know if there is a function that returns if there is an object in my hierarchy by checking its name or tag.

situation:

My character have 3 kinds of object to pickup, so I wanna check the name or tag of his hand child, how can I do it??

more ▼

asked Jan 06 '11 at 05:41 AM

Dinart Filho 1 gravatar image

Dinart Filho 1
134 18 20 28

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

5 answers: sort voted first

transform.Find();

more ▼

answered Jan 06 '11 at 05:53 AM

Eric5h5 gravatar image

Eric5h5
80.3k 42 132 521

I only want a confirmation about there is or isnt the object in the hierarchy OR an object with a tag, can transform.find return this information?? I have results to access things using transform.Find but not return if there is or not.

Thanks in advance!

Jan 06 '11 at 06:26 AM Dinart Filho 1

I just want something like "If (I am father of somebody called john){ doSomething();}

or just "if there is somebody called john in my game, do something"

Jan 06 '11 at 06:33 AM Dinart Filho 1

Please read the docs about transform.Find.

Jan 06 '11 at 09:04 AM Eric5h5

Eric is right: "If no child with name can be found, null is returned." You can use that.

Jan 06 '11 at 10:24 AM runevision ♦♦

You might try testing

if (ergue != null)

Jan 06 '11 at 07:49 PM DaveA
(comments are locked)
10|3000 characters needed characters left

This can be solved as a one off for all transforms by using an extension method. It also doesn't require you to pass in redundant parent info.

public static class Extensions
{
    public static Transform Search(this Transform target, string name)
    {
        if (target.name == name) return target;

        for (int i = 0; i < target.childCount; ++i)
        {
            var result = Search(target.GetChild(i), name);

            if (result != null) return result;
        }

        return null;
    }
}

Usage:

var foo = transform.Search("Foo");
more ▼

answered May 23 '12 at 08:34 AM

Mark Davis gravatar image

Mark Davis
121 1 2

Thanks to you I read up on Extension Methods and man I've been missing out on some cool stuff! I started using them and I love them!!!!

Aug 07 '12 at 01:53 AM ronronmx
(comments are locked)
10|3000 characters needed characters left

transform.Find(); will only search the immediate children. To search a whole hierarchy (children of children) for a transform of particular name - call a function like this (.js)

static function FindTransform(parent : Transform, name : String) : Transform
{
    if (parent.name == name) return parent;
    var transforms = parent.GetComponentsInChildren(Transform);
    for (var t : Transform in transforms)
    {
        if (t.name == name) return t;
    }
    return null;
}
more ▼

answered Jan 14 '11 at 03:08 PM

Andrew 10 gravatar image

Andrew 10
41 2

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

Andrew 10 -- great answer! Thanks. I have taken your code and adapted it (JS) to use tags

// FIND CHILD WITH TAG
function findChildWithTag(tagToFind:String) {
    return findChildWithTag(tagToFind, this.gameObject);
}
function findChildWithTag(tagToFind:String, startingObject:GameObject) {

    var childTransforms:Component[]       = startingObject.GetComponentsInChildren(Transform);
    for (var thisComponent:Component in childTransforms) { 
       var thisTransform:Transform     = thisComponent as Transform;
       if (thisTransform.gameObject.tag == tagToFind) {
         return thisTransform.gameObject as GameObject;
       }
    }
    return null;
}
more ▼

answered Apr 07 '12 at 06:06 PM

bladnman gravatar image

bladnman
16

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

Thank you Andrew 10 for your answer.

I just used to selectively turn off a few buttons in a virtual gamepad implementation:

//Hide all buttons' active/pressed state.
function SetupVirtualPad() {
    var transforms = gameObject.GetComponentsInChildren.<Transform>();
    for (var t : Transform in transforms)
    {
        if (t.gameObject.name.Contains("On") && t.gameObject.GetComponent.<Renderer>() != null) {
            t.gameObject.renderer.enabled =  false;
        }
    }
},
more ▼

answered Apr 12 '12 at 09:36 AM

Panajev gravatar image

Panajev
1

(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:

x413
x325
x208
x149
x143

asked: Jan 06 '11 at 05:41 AM

Seen: 13643 times

Last Updated: Aug 07 '12 at 01:53 AM