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 Flowen · Jul 04, 2014 at 06:22 AM · heirarchy

So, if transform.FindChild is the dumb way, what's the smart one?

I'm mucking around with a simple collectible card game prototype to push my skills with class architecture and code design, and while all is good so far, my curiosity is piqued. I've found threads over and over that state that traversing parent<->child hierarchies in code is a huge no-no for fast and re-usable code.

In my current case, I have a general card prefab that is a quad with two TextMesh children: CardTitle and CardText. The card prefab has a C# script with a method called AssignCard() that I call whenever I instantiate a new card prefab to tell the new prefab which card it is.

In AssignCard() I currently have the lines:

 transform.FindChild("CardTitle").gameObject.GetComponent<TextMesh>().text = _name;
 transform.FindChild("CardText").gameObject.GetComponent<TextMesh>().text = _text;

And they work. But I know there's a better way.

So, if you were in my shoes, how would you re-design this setup to get away from transform.FindChild and onward towards smart, beautiful code?

Comment
Add comment · Show 1
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 HarshadK · Jul 04, 2014 at 06:25 AM 0
Share

Caching the result of Find in your start function and accessing it whenever needed so you don't have to perform a Find every now and then.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by MakeCodeNow · Jul 04, 2014 at 06:25 AM

The best way is always to store a reference to the objects in question, either exposed as properties in the editor or as private variables that you assign to once at runtime. As always, the fastest way to do something is to not do it at all!

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 JPoenisch · Dec 04, 2019 at 08:43 AM

In my opinion the best way would be to have the fields

 [SerializeField] private TextMesh cardTitle;
 [SerializeField] private TextMesh cardText;

assign both on your prefab via the Inspector (drag&drop) and later use them

 cardTitle.text = _name;
 cardText.text = _text;

Skips all expensive Find and GetComponent calls on runtime entirely. Both references are simply already serialized and stored in the prefab and thereby already assigned on every spawned instance of this prefab right away.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Block Placement Help 2 Answers

Cube Counter 1 Answer

Inspector Assigned Variables Not In Both Scenes 1 Answer

MonoDevelops on the fly code formatting off 1 Answer

unity error error CS1525: Unexpected symbol `(', expecting `identifier' 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