Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Is Storing Rigidbody In a Variable And Using It To Access Rigidbody Good Practise

Discussion in 'Scripting' started by yasirkula, Aug 25, 2014.

  1. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,773
    Hi,

    I've heard from somewhere that if you access transform often in a script, then it is better to store it in a variable and access the transform via the variable. Though I've also heard that this is not the case for accessing the gameObject.

    I wonder if accessing rigidbody via a variable is faster than accessing it directly. I mean, is there a difference between these two lines of code in terms of performance:

    1- rigidbody.AddForce( Vector3.forward );

    2- rigidbodyVariable.AddForce( Vector3.forward );

    Thanks in advance.
     
  2. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    It's the same thing as far as I know. If the script is attached to the gameObject you are trying to access, then it is not needed to store the rigidbody. However, if you are accessing the rigidbody from a script attached to something else, it helps to store it to prevent having to find the gameObject with the respective rigidbody everytime you want to alter it.
     
    Last edited: Aug 25, 2014
  3. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    It's a get component lookup and you will see a difference. Lets not spread incorrect data. You should be caching all components.

    Storing the variable is faster.
     
  4. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    How much faster is it then? I never store the variable, since it seems redundant to do so in most cases and it uses memory as well I would guess.
     
  5. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,773
    Fraconte and Fluzing like this.
  6. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
  7. SkillBased

    SkillBased

    Joined:
    Aug 11, 2014
    Posts:
    141
    I'm reading up on cache this and cache that can't find any reference to exactly how cache'ing is done. How do I cache an object or component?
     
  8. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
  9. cranky

    cranky

    Joined:
    Jun 11, 2014
    Posts:
    180
    Components are classes, so caching one only stores a new reference. The memory footprint is absolutely negligible :).
     
  10. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    Code (csharp):
    1.  
    2. public class MyAwesomeScript : MonoBehaviour {
    3. private Rigidbody cachedRigidbody;
    4.  
    5. void Awake() {
    6. cachedRigidbody = rigidbody;
    7. }
    8.  
    9. void Update() {
    10. cachedRigidbody.AddForce(whatever);
    11. }
    12. }
    13.  
     
  11. cranky

    cranky

    Joined:
    Jun 11, 2014
    Posts:
    180
    This is how I cache. It's very simple and requires no extra modification to your code.

    Code (CSharp):
    1. public class Test : MonoBehaviour
    2. {
    3.     new private Rigidbody rigidbody;
    4.  
    5.     void Awake()
    6.     {
    7.         rigidbody = base.rigidbody;
    8.     }
    9. }
    This will hide the original rigidbody property behind your cached variable. In order to access the old inefficient property, you will need to use base.rigidbody instead. When you want to access your cached version, simply access it like you'd normally access the non-cached version.

    Hope this helps.
     
  12. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    Ooh. Clever.
     
  13. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    Although, if you're using that strategy, you should be doubly sure to backup your scripts when you update to 5.0. The convenience accessor properties are going away, and along with that there will be a script updater utility that will replace all "rigidbody" with "GetComponent<Rigidbody>()" in your project's scripts. I wouldn't count on this updater utility being smart enough to know the different between your 'new' accessor and the builtin ones, so it may replace all your now-efficient "rigidbody" references with inefficient "GetComponent<Rigidbody>()" references.
     
  14. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    Didn't read any of the above, but Mod Robert, and he's correct. You should be cacheing all component types. Unity 4.x doesn't cache them properly, although I hear that in Unity 5, they will be fixing this issue.