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 Hadrian · Dec 13, 2013 at 09:07 PM · c#variableoptimizationdeclaration

C# - Simple Optimization / Variable Declaration

Between declaring a class variable then setting it in the update function, or declaring and setting a variable inside of the update function, which is better coding practice? (Assuming variable will only be used in the update function.)

 public class SomeClass : MonoBehaviour {
 
     private float VarX;
 
     void Update () {
         VarX = 1f;
         ...
     }
 }

vs

 public class SomeClass : MonoBehaviour {
 
     void Update () {
         float VarX = 1f;
         ...
     }
 }

That is, should one constantly redeclare a variable, or declare it once then only change its value, given that Update() runs so often.

Comment
Add comment · Show 4
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 flaviusxvii · Dec 13, 2013 at 09:15 PM 1
Share

If the variable value doesn't need to exist beyond the scope of the Update function then I'd say declare it there. Don't worry too much about efficiency because the compiler is going to make the better choices for you generally.

You don't want to be allocating huge sums of memory every Update, don't get me wrong.. but one float isn't a big deal.

avatar image Hadrian · Dec 13, 2013 at 09:34 PM 0
Share

I'm more looking for a "general practice" sort of guideline, so that I don't have problems later on (when a lot of variables are being used).

avatar image stevethorne · Dec 13, 2013 at 09:45 PM 0
Share

For general practice what @flaviusxvii said is right. There is practically no optimization, the most important thing to keep in $$anonymous$$d is scope. If you're declaring your variables in the lowest scope possible, then you're going to be better off. If you need to use it in multiple places in your script, then declare it at the top of your script. If you need the variable throughout your update function but not in any other functions, then declare it in your update function.

As long as you have good program$$anonymous$$g practices when it comes to scope, then optimization with variable scope wont be an issue. Just be careful not to set the variable more times than you need to. If it's a variable that stores a lot of information and declaring it in your Update loop requires you to repopulate that variable every frame, then consider declaring it globally and setting the information less often.

avatar image EX_Darius · Dec 14, 2013 at 12:58 AM 0
Share

Ive done some research on optimization of code and came across this website : http://www.tantalon.com/pete/cppopt/main.htm

Eventhough this is meant for C++ , im pretty sure the same rules apply to every other language out there. good luck!

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Hadrian · Dec 13, 2013 at 11:04 PM

Thanks for the comments, I'll mark this as answered.

If the variable value doesn't need to exist beyond the scope of the Update function then I'd say declare it there. Don't worry too much about efficiency because the compiler is going to make the better choices for you generally.

For general practice what @flaviusxvii said is right. There is practically no optimization, the most important thing to keep in mind is scope. ...

If it's a variable that stores a lot of information and declaring it in your Update loop requires you to repopulate that variable every frame, then consider declaring it globally and setting the information less often.
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

18 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

Related Questions

Trying to call a variable from another script in C# 1 Answer

Loading a Constant Array Of Integers 0 Answers

Why can't unity see my variable declaration? 1 Answer

Most efficient scope of declaring variables 1 Answer

Multiple Cars not working 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