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 /
avatar image
0
Question by justinhj · Nov 30, 2014 at 06:08 PM · transformfind

Transform.Find failing to find named child in clone

In my code I create a UI element which is a hierarchy of objects made into a prefab. I then instantiate at runtime. I then want to find child objects by name so that I can modify them in the script. When I call Transform.Find it fails to find the object. I wrote a function that builds a dictionary of the children by name, and that works. I don't understand why the built in function is not working for me.

         GameObject clone = (GameObject)  Instantiate(levelFab);


 //            Transform text = clone.transform.Find("LevelName");
 
             FindChildren(clone.transform, d);
             
 
     private void FindChildren(Transform transform, Dictionary<String, GameObject> d)
     {
         foreach(Transform t in transform)
         {
             d.Add(t.name, t.gameObject);
 
             Debug.Log("adding " + t.name);
 
             FindChildren(t, d);
         }
         
     }
Comment
Add comment · Show 2
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 b1gry4n · Nov 30, 2014 at 06:14 PM 0
Share

http://answers.unity3d.com/questions/689549/find-child-objects-by-name.html

I suspect maybe you are calling this function from a script on a different object than the children object's parent. If that is the case, you are searching all the children of the object calling this function. Try changing the term "transform" to something else and see if that works.

  private void FindChildren(Transform transform, ... etc

to

  private void FindChildren(Transform parentTrans, ... etc
      //...
      foreach(Transform t in parentTrans)
      //...
avatar image justinhj · Nov 30, 2014 at 08:56 PM 0
Share

$$anonymous$$y function is working though. It is the built in one that is not, and they both are called on the same object.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by jenci1990 · Nov 30, 2014 at 10:43 PM

Use transform.FindChild("ChildName");

Comment
Add comment · Show 4 · 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 justinhj · Nov 30, 2014 at 11:07 PM 0
Share
         Transform test = clone.transform.FindChild("LevelName");

returned null

avatar image jenci1990 · Nov 30, 2014 at 11:37 PM 0
Share

You can't find children's child. You have to know the hierarchy. example: my first child is "child1" and this child is "child2". You can find it by this:

 Transform test = transform.FindChild("child1/child2");

but

 transform.FindChild("child2");

not work!

avatar image jenci1990 · Nov 30, 2014 at 11:39 PM 0
Share

Your function is the best way!

avatar image justinhj · Dec 01, 2014 at 03:55 AM 0
Share

Ah, I understand. Thanks for the explanation jenci1990. I shall stick with my function :)

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

transform.Find(string)? 2 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

C# The call is ambiguous between the following methods or properties 1 Answer

Snap Back? 1 Answer

carl.transform.position = characterPosition -- how to set to this specific position but -3 on x axis? 2 Answers


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