Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
1
Question by cgeopapa · Dec 31, 2015 at 06:01 PM · c#uigameobjectgetcomponentfind

What to use instead of GameObject.Find and GetComponent

Hello!

So what Ive learned throught my time in Unity is not to use GameObject.Find and GetComponent(). So what should I use instead?

Lets say I have a UI.Text component on a GameObject which is child of the canvas and I want to set the text value of it equal to my player's health through the script I have attached on my player, who is obviusly not a child of the canvas. What is the most efficient way to make this?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Socapex · Dec 31, 2015 at 06:45 PM

 [SerializeField] PlayerHurtScript playerHurtScript;
 
 // Somewhere
 playerHurtScript.DoSomething(potato);

Drag the Player object onto the new field.

Note that GetComponent isn't bad in and of itself (especially with auto-caching). I agree Find is the devil, but mostly because it can silently break your game.

You can also have a "master" singleton that gives out references to important objects like player/score etc. You still drag the stuff on him though.

Comment
Add comment · Show 2 · 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 Dave-Carlile · Dec 31, 2015 at 06:47 PM 1
Share

To add a little bit - if you're just hooking up a couple of things in a Start method there isn't really anything wrong with using these functions. You don't want to run them each frame though.

avatar image Socapex Dave-Carlile · Dec 31, 2015 at 06:54 PM 0
Share

Exactly. I wouldn't worry to much about GetComponent (even in loops, because caching).

Find is bad, and it is a good thing to stay away from it. I used to say "use it reasonably", but after seeing so much atrocious production code, ppl using find in triple loops and all, I say it is a good decision to never use Find. Unity provides many alternatives.

It is often fun and interesting to theorize and brainstorm alternatives to Find anyways :)

avatar image
1

Answer by callen · Dec 31, 2015 at 06:58 PM

Last time I checked the decompiled source, I saw that GetComponent is using an internal dictionary of Type->Components to make subsequent calls more quickly. (I think that's what Socapex's 'auto-caching' comment was referencing but not sure). Dictionaries should be fast enough in C# to not worry about calling multiple GetComponent()s every frame.

This doesn't necessarily happen with the GetComponentInChildren calls though, so I'd be more careful and keep a variable with those results. I've never used Find really, because I always heard it was the devil and I don't like keeping const string values in my code when I don't have to.

Most efficient way to do what you requested? I'd probably just put a script on the UI.Text itself that looks like this:

 class HealthDisplay : MonoBehavior
 {
   //assigned in the editor, through drag-n-dropping your player object to it
   public PlayerScript Player; 
   void Update(){
     GetComponent<Text>().text = Player.health + " HP";    
   }    
 }
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

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

Value doesn't change in game when updated. 2 Answers

Why Does Prefab Loose UI elements on Scene Change 0 Answers

How do I shorten a gameObject.GetComponent 1 Answer

Move gameObject on UI Button Press 2 Answers

How do I get the type of a Monobehaviour? 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