Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jul 04, 2014 at 12:52 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
10
Question by Dinart Filho 1 · Jan 06, 2011 at 05:41 AM · childtaghierarchyfindname

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??

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

  • Sort: 
avatar image
15
Best Answer

Answer by Eric5h5 · Jan 06, 2011 at 05:53 AM

transform.Find();

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dinart Filho 1 · Jan 06, 2011 at 06:26 AM 0
Share

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!

avatar image Dinart Filho 1 · Jan 06, 2011 at 06:33 AM 0
Share

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"

avatar image Eric5h5 · Jan 06, 2011 at 09:04 AM 0
Share

Please read the docs about transform.Find.

avatar image runevision ♦♦ · Jan 06, 2011 at 10:24 AM 0
Share

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

avatar image DaveA · Jan 06, 2011 at 07:49 PM 1
Share

You might try testing

if (ergue != null)

Show more comments
avatar image
17

Answer by Mark-Davis · May 23, 2012 at 08:34 AM

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");
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ronronmx · Aug 07, 2012 at 01:53 AM 0
Share

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

avatar image alienmax22 · Jul 04, 2014 at 12:45 PM 0
Share

wow, that's the answer!!!!

avatar image sagivo · Jan 17, 2015 at 09:06 AM 0
Share

beautiful

avatar image
5

Answer by Andrew 10 · Jan 14, 2011 at 03:08 PM

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;
}

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image yuhmik · Oct 29, 2013 at 09:00 PM 0
Share

In C# one can use System.Linq. Then all could be done in one line: transform.GetComponentsInChildren().FirstOrDefault(t => t.name == "whats_your_name")

avatar image
1

Answer by bladnman · Apr 07, 2012 at 06:06 PM

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;
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Panajev · Apr 12, 2012 at 09:36 AM

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;
         }
     }
 },
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to call a child by tag or name 1 Answer

GameObjectWithTag Child 1 Answer

Get object child list with tag 2 Answers

Finding a child without knowing its name? 1 Answer

How can I find a unique sibling/parent/child in a specific hierarchy and not from all the scene? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges