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 chirhotec · May 15, 2014 at 07:10 PM · optimizationcomponentmonobehaviour

Caching a Monobehaviour Component's reference properties (rigidbody2d, specifically)

I'm in a live training session (not Unity official), and the instructor is saying that everytime you call this.rigidbody2d, the rigidbody2d getter is essentially calling GetComponent(). This of course is pretty slow, so he's recommending we cache the reference on Awake().

Essentially, he is saying this:

 public class MyClass : MonoBehaviour {
     private Rigidbody2D cachedRigidBody2D = null;
 
     private void Awake() {
         this.cachedRigidBody2D = GetComponent<Rigidbody2D>();
     }
 
     private void FixedUpdate () {
         cachedRigidBody2D.AddForce (Vector2.up);
     }
 }

Is more efficient than this:

 public class MyClass : MonoBehaviour {
 
     private void FixedUpdate () {
         this.rigidbody2d.AddForce (Vector2.up);
     }
 }

I've been using Unity for years and have never heard of this. Seems like a pretty important optimization to always make given how frequently you'd typically reference these built-in component properties. Does anyone know if this is accurate?

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 theredace · May 15, 2014 at 08:44 PM 0
Share

In the first caching example, I don't think you even need to use GetComponent, do you? Can't you just say:

 private void Awake() {
         cachedRigidBody2D = rigidbody2D;
     }

I mean, it's an inherited variable so no need to waste the extra time to perform GetComponent, even in the Awake method. Then your cached variable will run slightly faster than the pre-defined inherited variable, as chirhotec showed below.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by chirhotec · May 15, 2014 at 07:35 PM

Okay, I just did some some tests, and the cached version is actually faster! (though referencing this.rigidbody2d isn't nearly as slow as GetComponent();

For details, here's the test: I set up a loop in the Update() function that runs 1 million times per frame.

  • Test 1: no loop => ~80 FPS

  • Test 2: Using the cached reference [cache2 = cachedRigidBody2D;]=> ~80 FPS, maybe slightly slower

  • Test 3: Using the built in reference [cache2 = this.rigidbody2D;] => ~45 FPS

  • Test 4: Using GetComponent [cache2 = GetComponent();] => ~5 FPS

The More You Know!

===============*

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

Get OnMouseUp event in other object's script 1 Answer

Speed and efficiency of enabling/disabling component? 1 Answer

Show enabled flag on custom components that don't use Unity event functions? 0 Answers

Copying a Component's values without a new Game Object 1 Answer

Create a method that gets called everytime something happens, passing a Transform component to it 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