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
1
Question by ina · May 13, 2011 at 06:38 AM · mobilegetcomponentcomponentfindassign

Better to assign components to variables or reference directly each time

I am still trying to better understand how to optimize an Unity mobile app.

Is it better to do something like this - Example A - where the GetComponent is assigned at Start, rather than explicitly referred to, each time, as shown in Example B

Example A

var pTransform:transform;
function Start(){
   pTransform=GameObject.Find("player").transform;
}
function Update(){
   pTransform.position+=Vector3(1,0,0);
   pTransform.localScale*=2;
}

Example B

var player:GameObject;
function Start(){
   player = GameObject.Find("player");
}
function Update(){
   player.transform.position+=Vector3(1,0,0);
   player.transform.localScale*=2;
}
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

3 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Ashkan_gc · May 13, 2011 at 07:13 AM

it's always a better idea to cache everything. when you use GetComponent or properties like transform and rigidbody, unity have to go and walk in a tree and find the component for you. it's not much slow but can make a difference if it's called multiple times a frame and if it's on a mobile phone. GameObject.Find is really something that you should avoid in methods other than Start and Awake and any other method that is called once. don't call it in update or OnCollisionEnter or so unless you have to do so. always find all references at start or even create a list and tell every object required to add it'self to it. Dictionary can be useful too.

take a look at this video by the CTO from unite 07 for more info about caching.

if you want to, you can test it too. use both methods above with 1000 objects and take a look at fps or your profiler if you have pro to see how much more time the script takes to execute.

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

Answer by Dreamer · May 13, 2011 at 07:00 AM

This is a tricky question. I tell you what, why don't you do an experiment:

Example A

var pTransform:transform;
function Start(){
   pTransform=GameObject.Find("player").transform;
}
function Update(){
   **for(var i:int=0;i<100000;i++)**{
   pTransform.position+=Vector3(1,0,0);
   pTransform.localScale*=2;
   }
}

Example B

var player:GameObject;
function Start(){
   player = GameObject.Find("player");
}
function Update(){
   **for(var i:int=0;i<100000;i++)**{
   player.transform.position+=Vector3(1,0,0);
   player.transform.localScale*=2;
   }
}

Check the run-time fps to see which one is faster.

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

Answer by CHPedersen · May 13, 2011 at 06:49 AM

Yes, absolutely! Same with things like calls to GameObject.Find(). It's always better to cache references instead of looking them up each time. I'm not familiar with what Unity does behind the curtains, but it isn't unreasonable to expect that it stores GameObjects in some kind of master-list, then Components of GameObjects in a list for each GameObject. This means you'd have to traverse the lists for the target component every time you do a call to GetComponent or a search through all GameObjects with a comparison against a GameObject's name every time you do a call to GameObject.Find().

Edit: I just had a better look at your Examples A and B. In those cases, the optimization is probably non-existant since you're refering to the gameobject's transform, which doesn't require a call to GetComponent.

Edit2: Take a look at http://unity3d.com/support/documentation/ScriptReference/GameObject.Find.html where the documentation also recommends caching. :)

Comment
Add comment · Show 2 · 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 Ashkan_gc · May 13, 2011 at 07:06 AM 1
Share

actually accessing transform component is no different. it's just a property that in it's get {} the code uses GetComponent.

avatar image CHPedersen · May 13, 2011 at 07:42 AM 0
Share

Oh. O_O I didn't know that. Then I guess there is a bit of overhead to be saved from caching transform.

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

No one has followed this question yet.

Related Questions

Why can't I change another script's variable with this script? Thanks, 1 Answer

How do I find an external component from inside a class? 0 Answers

Checking if a component has been removed or not at runtime 1 Answer

GetComponent in parent question 3 Answers

Acessing a Script Instance from Another Object 3 Answers


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