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
1
Question by EccentricDuck · Aug 02, 2013 at 01:06 AM · gameobjecttransformmesh

Problems with Transform.Find and GameObject.find

Costume Swapping: Why can I not find certain objects in the hierarchy with Transform.Find or GameObject.Find, yet I can find them fine in the hierarchy search bar by the exact same name?

So I'm trying to find mesh Transforms on a model in the hierarchy (imports as a hierarchy of GameObjects) and I need to find particular children and turn them on/off. I'm doing this so I can do dynamic costume swapping for our game. The turning on/off of meshes works just fine (using MeshRenderer.enabled). So does finding SOME of the objects in the hierarchy (they're all deeply nested children of the GameObject with the Behaviour I'm searching from).

For instance, when I search for "myCharacterName_baton", it finds it using either GameObject.Find or Transform.Find (as it should). But when I search for "myCharacterName_hat", it can't find it. Both "myCharacterName_baton" and "myCharacterName_hat" are searchable from the Hierarchy Search Bar in the editor at run-time. They're also searchable not at run-time (nothing there is dynamically added/removed). Is this a known bug? Is there something horrendously simple I've overlooked? Any troubleshooting help would be appreciated.

Here's an example of what the code looks like (including the debug statements I'm using to confirm what's happening):

 foreach (string costumePiece in costumeSet)
                 {
                     Transform pieceTransform = transform.Find (costumePiece);
                     Debug.Log (costumePiece + "; transform: " + pieceTransform, this);
                     GameObject go = GameObject.Find (costumePiece);
                     go.GetComponent<MeshRenderer> ().enabled = false;
                 }

It always breaks on the "GameObject go = GameObject.Find (costumePiece)" line because the Transform is null... well, always when I'm searching for the hat, it finds the baton just fine.

I realize too that it would be possible to create direct references to all those components and enable/disable them that way. Unfortunately, that's not very convenient for our artist because those references get destroyed every time he re-imports the models. Dynamically grabbing them at run-time creates a much better pipeline for him.

Comment
Add comment · Show 6
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 Benproductions1 · Aug 02, 2013 at 01:07 AM 1
Share

Please pick a $$anonymous$$ORE APPROPRIATE title!. CAPS LOC$$anonymous$$ will THROW people off FRO$$anonymous$$ ANSWERING your QUESTION :)

avatar image robertbu · Aug 02, 2013 at 03:14 AM 0
Share

Are you just turning the renderer on and off, or are you deactivating the game objects? Neither GameObject.Find() nor Transform.Find() will find inactive game objects. If that is your problem, Google "unity3d find inactive game objects" for many hits concerning hacks and workarounds for the problem.

avatar image EccentricDuck · Aug 02, 2013 at 04:28 PM 0
Share

Sorry about the title - I was feeling incredibly frustrated. I'm just disabling the mesh renderer. The gameobjects are all active in the scene and their parents up the hierarchy are all active.

avatar image perchik · Aug 02, 2013 at 04:34 PM 0
Share

Can you perhaps post a screenshot of your hierarchy? I'm wondering what behavior you're searching from and it's relation to the desired objects

avatar image EccentricDuck · Aug 02, 2013 at 05:33 PM 0
Share

Here yah go: http://i.imgur.com/PlTbmIh.png

Those are the two GameObjects I was referring to. Both have $$anonymous$$eshRenderers on them. Casey_mesh_baton is findable while Casey_mesh_hat isn't.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by DaveA · Aug 02, 2013 at 04:57 PM

Transform.Find does not recursively search, to my knowledge. Probably the editor has a small script which does. So you might do something like this (not tested):

 Transform FindChild (string name, Transform t)
 {
   if (t.name == name)
    return t;
   foreach (Transform child in t)
   {
     Transform ct = FindChild (name, child);
     if (ct != null)
      return ct;
   }
   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

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

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

C# Preserving GameObjects' Previous Meshes 1 Answer

C# Array of OtherArray's Meshes 1 Answer

C# Function Transform to Gameobject Convert 1 Answer

C# GameObjectList not Setting Parent 0 Answers

C# Check If Gameobject is within Collider 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