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 itamarperets30 · Nov 04, 2020 at 04:10 PM · methodsreference-other-object

Do cached references in methods also increase performance?

,I know how usually if you reference another object or component more than once then it's good practice to cache them as variables to increase performance. However, I've only ever done this on a global scale (at the top of the script). I'm wondering if caching the references within a method would have the same effect or if the variable's value would be updated every time the method is called thus being pretty much useless for performance.

Here's an example: using UnityEngine;

 public class Stuff : MonoBehaviour
 {
     // Option 1
 
     Rigidbody rb;
 
     void Awake()
     {
         rb = GetComponent<Rigidbody>();
         // Later on do stuff using rb
     }
 
     // Option 2
 
     void Update()
     {
         Stuff();
     }
 
     void Stuff()
     {
         Rigidbody rb = GetComponent<Rigidbody>();
         // Do stuff using rb
     {
 }
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
2
Best Answer

Answer by Bunny83 · Nov 04, 2020 at 07:05 PM

Of course it will and you should do it when appropriate. Common examples are components which you can not cache in the class itself because the depend on external information. For example inside OnCollisionEnter you may want to access a component on the other object you collided with. So you can not save a reference in the class and you have to use GetComponent. However you only ever want to use GetComponent once per component inside the method.


Note that the improvements are in the micro optimisation scale as GetComponent isn't as bad as it's often stated. It litterally just searches through the internal list of components on a gameobject on the native side. Usually you only have a hand full of components on a single gameobject. So looking up a particular component is just a matter of doing a few loop iterations and checking the type.


Though it's actually more a matter of style. This applies to all sorts of other cases. Like accessing fields of a class that is stored in an array. Instead of doing

 for (int i = 0; i < someArray.Length; i++)
 {
     someArray[i].fieldOne = 5;
     someArray[i].someOtherField = 6;
     someArray[i].ABCD = 42;
 }

it's generally more readable and slightly better for performance to do:

 for (int i = 0; i < someArray.Length; i++)
 {
     var obj = someArray[i];
     obj.fieldOne = 5;
     obj.someOtherField = 6;
     obj.ABCD = 42;
 }

This of course has an even greater effect of the access of the instance is more complex. It also gives you a chance to choose a better local variable name so the intention of the code becomes more clear to the reader. I don't quite understand why some people are obsessed with one-liners. In most cases they are hard to read, harder to debug and don't provide any actual advantage except saving a few lines of code. Unless you apply for the International Obfuscated C Code Contest, keep your code readable and debuggable. Having a one liner means if you have any kind of error in that one line makes it much harder to find the error. Splitting the code into multiple lines helps to isolate the issue.

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

137 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Simply calling a method from another script :( 1 Answer

Make method variable show up in Inspector 1 Answer

My Method won't take in a class. 2 Answers

Generalized call of specific methods 1 Answer

grab variable from an instance of a game object? 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