Search Unity

  1. We've closed the job boards. If you're looking for work, or looking to hire check out Unity Connect. You can see more information here.
    Dismiss Notice
  2. We're running great holiday deals on subscriptions, swag and Asset Store packages! Take a peek at this blog for more information!
    Dismiss Notice
  3. Check out our Unite Austin 2017 YouTube playlist to catch up on what you missed. More videos coming soon.
    Dismiss Notice
  4. Unity 2017.2 is now released.
    Dismiss Notice
  5. The Unity Gear Store is here to help you look great at your next meetup, user group or conference. With all new Unity apparel, stickers and more!
    Dismiss Notice
  6. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  8. Unity 2017.3 beta is now available for download.
    Dismiss Notice

Confirmation : no cache needed for "gameObject" property ?

Discussion in 'Scripting' started by Nems, Apr 29, 2012.

  1. Nems

    Nems

    Joined:
    Jul 18, 2011
    Posts:
    65
    Hi,

    I would like to access the GameObject of a component and there is a property for that :
    http://unity3d.com/support/documentation/ScriptReference/Component-gameObject.html

    Best practices advice to cache element through the Awake method, instead of using component's properties.
    Because behind the scene, property (like "transform") performs a : GetComponent call.

    But, GameObject is not even a component (inherit from Object), so I presume there is no need to cache it in a private var.

    Can you confirm that fact ??

    I am worried about performances and I need a process as cheap as possible to check the layer of an object after a RayCast. The only way I found is to retrieve the GO, then use the layer property.
     
  2. npsf3000

    npsf3000

    Joined:
    Sep 19, 2010
    Posts:
    3,832
  3. Tobias J.

    Tobias J.

    Joined:
    Feb 21, 2012
    Posts:
    423
    I haven't gotten around to using it yet, but I certainly will: Profiler from the wiki

    Seems very simple. Does the job and no more.
     
  4. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,350
    Was wondering about this too. Normal advice is to cache components rather than use the properties over and over, but like the OP says, a GameObject is not a component, so is there any need to cache gameObject?

     
  5. AkilaeTribe

    AkilaeTribe

    Joined:
    Jul 4, 2010
    Posts:
    1,149
    I thought there would be some kind of optimization regarding this (like the Lazy class in C# 4.0). Are you sure about this ?
     
  6. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,350