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 /
  • Help Room /
avatar image
0
Question by GetColor · Aug 27, 2013 at 12:32 AM · getcomponent

is gameObject.Getcomponent an expensive call?

I'm working on my second iOS project, and I have a few destructible environment objects I'm working with. Each part of the destructible object has to have a script reference to every other part of the destructible object. I have a few more of these destructible objects to code, but I wanted to stop and make sure that calling gameobject.getcomponent("name of object") wasn't too expensive (it's currently only being called in the start function). Basically, for this object that breaks into 4 pieces, each of those 4 pieces has a GameObject.Getcomponent to find the script of the other 3 pieces in the object. Should I be worried about this clogging up my iOS game?

Any help is greatly appreciated :D

  • Sussy

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

3 Replies

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

Answer by getyour411 · Aug 27, 2013 at 12:35 AM

Try this thread

http://answers.unity3d.com/questions/15451/which-is-faster-gameobjectfind-or-gameobjectfindwi.html

Sounds like you are doing that in start and cacheing the hook which is the way I usually see it recommended. You did put your example in "..." as if it were a string so I'll add this from here:

Do not use the version of GetComponent that takes a string. When you call GetComponent without a string the return value is already of the correct type and you can access your custom variables and methods. If you call GetComponent("TargetScriptName") you will get your script, but the compiler doesn't know what type it is so you cannot access your custom variables and methods [unless you manually typecast]

Just a tip worth remembering.

Comment
Add comment · Show 5 · 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 GetColor · Aug 28, 2013 at 09:55 PM 0
Share

I'm using C#. I'm having issues using the getcomponent without using the string version. I currently have it like so, glass1Script = glass1.GetComponent; but it's giving me an error. Before I was using the string and then casting it and it worked, but I figured there was an easier and more efficient way to do that.

and I would look at the unity scripting reference, but the c# pages are broken since they updated the site

avatar image TrickyHandz · Aug 28, 2013 at 10:01 PM 2
Share

The syntax for calling a typed reference to a component in C# is as follows:

 ClassName variableName = object.GetComponent<ClassName>();
avatar image GetColor · Aug 28, 2013 at 10:18 PM 0
Share

Those damn parenthesis!!! I've always had to make a reference to the object, then a separate reference to the scripts on those objects... this saves SO much time!!!

avatar image getyour411 · Aug 31, 2013 at 03:48 AM 0
Share

Would you please mark this as answered? I think the tip Tricky provided resolved the last issue.

avatar image fafase · Aug 31, 2013 at 05:54 AM 0
Share

http://unitygems.com/quick-benchmark/

avatar image
-2

Answer by Aresion · Mar 25, 2019 at 07:12 PM

"Premature optimization is the root of all evil"

Don't bother cacheing a reference to your components. It will maybe save an unnoticeable time, but it will be a horrible mess if the component gets destroyed, or changed.

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 TreyH · Mar 25, 2019 at 07:19 PM 0
Share

I don't think you can equate caching a component to writing optimized and difficult-to-maintain code.

Unless "changed" means "replaced", this makes very little sense in general. Aside from the case of your component being destroyed (which you would check for identically?), seeing dozens of *.GetComponent<Something>().* polluting a function declaration is almost unreadable by itself.

avatar image Owen-Reynolds TreyH · Mar 25, 2019 at 09:23 PM 0
Share

Caching is slightly more error-prone, slightly harder to read, and slightly more difficult to change. The error part is you can forget to initialize it, and as the answerer notes, changing the object and forgetting to update the reference. I've done that. While getComponent(x) may not look pretty, it's more readable than a variable componentReference.

If the component is't used much, caching may even make you run slower because of the extra variable (but either way, it's going to be barely noticeable).

avatar image TreyH Owen-Reynolds · Mar 25, 2019 at 10:10 PM 0
Share

Yeah, those are fair points and I'm conflating readability with eyesores.


I think the only "running slower" I'll agree with though is my lifespan after scrolling past an ocean of UI component references on my way through someone's menu system.

avatar image
0

Answer by psychentist · Aug 27, 2013 at 12:38 AM

Not really. If you're that concerned about it, you can assign the resulting component to a variable so it can be directly addressed. That will save you about three flops. I really doubt you would ever notice any difference unless you're addressing it every update or something.

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

21 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

Related Questions

Freeze Rigidbody 1 Answer

Getting typename from collection. 0 Answers

ERROR CS0119 C# GetComponent< > 1 Answer

Best way to reach a component or script's function? 1 Answer

Semicolon expected before end of line 0 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