Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 ina · Jun 11, 2011 at 06:19 PM · gameobjecttransformoptimizationcomponentglobal

Assigning to global transforms of current gameobject

For optimization, is it necessary to assign to global the transforms of the gameobject the current script is in?

i.e., start(){var t:transform;} update(t.position+=Vector3(x,y,z))

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
0
Best Answer

Answer by Peter G · Jun 11, 2011 at 07:30 PM

Actually, your code won't work ;)

 function Start () {
      var t = transform;
      //local variable declaration.  Only accessible from within this function
      //goes out of scope when the function returns.
 }
 
 function Update () {
       t.position += Vector3();
       //What's "t"
 }

Sorry to point that out, but its an important mistake, Caching the transform to a local variable will not do you any good.

  1. It will go out of scope at the end of the function. That means that it can't be accessed any more and it will be marked for garbage collection. So now you're back where you started. :)

  2. You're just allocating extra memory. Creating a reference type like that puts memory on the heap.

  3. No other functions can use it. Like #1, you will cache the variable, just not in a useful place. Given it is a local variable, it isn't accessible from outside the function.

But, caching component look ups is a good optimization to perform. It saves Unity from having to find the component attached to the game object every time that you access it. This page explains it fairly well.

 var t : Transform;
 function Start() {
     t = transform;
     //cache a reference to the Transform so that Unity doesn't need to find it again
 }
 function Update() {
     t.position += Vector3();
 }


Comment
Add comment · Show 1 · 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 ina · Jun 11, 2011 at 11:32 PM 0
Share

typo. mea culpa + thanks!

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

2 People are following this question.

avatar image avatar image

Related Questions

Are .gameObject and .transform both using GetComponent() in the background? 1 Answer

Component without gameObject 2 Answers

How to Change Background Sprite at run time ? 0 Answers

Transform vs Gameobject 3 Answers

Calling gameobject.transform vs. just calling transform directly - Performance negligible? 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