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
3
Question by Essential · May 18, 2013 at 02:51 AM · gameobjecttransformperformanceoptimization

Are .gameObject and .transform both using GetComponent() in the background?

I know .transform usage should be cached for optimization. I can get around that by directly storing the Transform of the object so I can just say:

 var myObject : Transform;
 myObject.position = Vector3.zero;

However, if I also need to access the GameObject (in this case, to set it as active/inactive), does using myObject.gameObject also use a hidden GetComponent lookup as .transform does, or is .gameObject a special case?

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

1 Reply

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

Answer by Eric5h5 · May 18, 2013 at 04:11 AM

A GameObject is not a component (it's a container for components), so no, there is no possibility of using GetComponent in that case. The .transform shortcut is not actually using GetComponent either...there are three levels, in order of slowest to fastest: 1) GetComponent(Transform), 2) .transform, and 3) cached Transform such as your code example. Although "slowest" is relative; in reality there's not a huge difference, and while caching it certainly doesn't hurt, it may not result in any noticeable gains, unless you're really accessing that component a lot.

Comment
Add comment · Show 6 · 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 Essential · May 18, 2013 at 09:06 PM 0
Share

Ah, so I shouldn't be as worried about using accessor shortcuts as the Unity optimization leads me to believe? I'll have to reconsider the way I'm coding right now. So much conflicting info. Thanks!

avatar image whydoidoit · May 18, 2013 at 09:29 PM 6
Share

So to put some scale on it:

In this first comparison a cached transform takes 0ms on my computer.

transform = 0.000622ms

GetComponent(typeof(Transform)) = 0.001009ms

GetComponent< Transform>() = 0.00157ms

So clearly if you were looking a 1000 in a few loops every frame it would become worth caching (on my computer - i$$anonymous$$ac 3.4Ghz), it might be significantly more on worthwhile on a mobile device.

It appears:

GetComponent(Type) is 1.6x slower than transform

GetComponent< Transform>() is 2.5x slower than transform

In a second test, to compare cached transforms against transform I access the name property and that yields a cost of:

0.000817ms cost per usage of transform versus a cached transform. In this case making the non-cached code 20% slower than the cached version.

avatar image whydoidoit · May 18, 2013 at 09:36 PM 3
Share

I should also point out that using .position is 19% slower than .localPosition when the object has no parents, rising to 28% with one parent and increasing for every level of parent above.

avatar image Eric5h5 · May 18, 2013 at 11:19 PM 1
Share

Some of the conflicting info exists because this stuff (that is, GetComponent) used to be slower than it is now. But at some point they optimized it. Fancy that. ;)

And yes, whydoidoit re$$anonymous$$ds me that it's actually 4 levels, since the generic GetComponent is a bit slower: 1) GetComponent.(), 2) GetComponent(Transform) (and the long-winded C# equivalent), 3) .transform, 4) cached version.

avatar image ABerlemont · Oct 22, 2014 at 02:39 PM 0
Share

Does someone knows why transform is slower than cached transform ?

Show more comments

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

18 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

Related Questions

Calling gameobject.transform vs. just calling transform directly - Performance negligible? 1 Answer

Assigning to global transforms of current gameobject 1 Answer

Transform vs Gameobject 3 Answers

Mesh.CombineMeshes does not work when mergeSubMeshes=false 2 Answers

Which is better performance-wise? 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