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
3
Question by PaxForce · Feb 19, 2014 at 08:30 AM · variablesdeclaration

best place to declare variables in the script

In one of Unity’s projects called Roll-a-Ball... http://unity3d.com/learn/tutorials/projects/roll-a-ball ...they declare the float moveHorizontal and moveVertical variables inside the FixedUpdate function. Like this:

 public class PlayerControler : MonoBehaviour {
     
     public float speed;
     public GUIText countText;
     public GUIText winText;
     private int count;
     ...etc
 
   
     void FixedUpdate()
     {
             float moveHorizontal = Input.GetAxis ("Horizontal");    
         float moveVertical = Input.GetAxis ("Vertical");
         
         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
         
         rigidbody.AddForce(movement * speed * Time.deltaTime);
     }

Is it a good place? Isn’t the FixedUpdate declaring these variables on each fixed update? Wouldn’t it bebetter to declare those variables before the Start function is called? Like this:

 public class PlayerControler : MonoBehaviour {
     
     public float speed;
     public GUIText countText;
     public GUIText winText;
     private int count;
     private float moveHorizontal;
     private float moveVertical;
     ...etc
   
     void FixedUpdate()
     {
         moveHorizontal = Input.GetAxis ("Horizontal");    
         moveVertical = Input.GetAxis ("Vertical");
         ...etc
         

Comment
Add comment · Show 1
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 DajBuzi · Feb 19, 2014 at 08:41 AM 0
Share

Variables declared inside function/method will be accesable only inside this function. If you wouldnt use those vars outside this method it's better that way.

2 Replies

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

Answer by whydoidoit · Feb 19, 2014 at 08:38 AM

No it's fine to declare it in the function. Declaration of variables that are local to a function allocates them on the stack by moving the stack pointer (done at compile time), it uses less memory as the variables are only temporary and has no performance hit.

Comment
Add comment · Show 6 · 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 whydoidoit · Feb 19, 2014 at 08:40 AM 0
Share

Note that local variables are not initialized so they must be set before being read and will not have a default value. The compiler flags any attempt to use a local variable before setting its value.

avatar image PaxForce · Feb 19, 2014 at 09:06 AM 0
Share

so are you saying it is even better to declare them INSIDE the function? is it more resource efficient that way? requires less memory? and they are NOT being declared on each fixed update?

avatar image whydoidoit · Feb 19, 2014 at 09:09 AM 0
Share

Yes that is what I am saying. They are being declared (space allocated on that stack each FixedUpdate) but as they are just simple variables this takes no extra time.

avatar image PaxForce · Feb 19, 2014 at 09:24 AM 0
Share

okay thanx. answer accepted.

avatar image fafase · Feb 19, 2014 at 09:32 AM 3
Share

Also, it removes any possibilities of misused of the variable. Working as a $$anonymous$$m often leads to programmers not documenting enough. Then you put your two variables as global, and someone else comes to develop a method where for some reasons, he decides to use them, what you did is now all wrong.

As my rule of thumb, your program is like a zoo, each animal/variable is confined to his cage/scope, if you open it all, here comes the pain (including for the staff).

Show more comments
avatar image
0

Answer by PaxForce · Feb 22, 2014 at 12:21 PM

I found a useful video that pretty much answers my question :) It is explained using C++ but it's easily applicable to C#.

click here to navigate to the video (YouTube)

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

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

Related Questions

Which variable can be implicitly declared? 1 Answer

GameObject.Find either can't be called or is in the wrong scope. 0 Answers

javascript: declaring a variable in 'while' loop 2 Answers

Rewriting C# to JavaScript 2 Answers

Convert class of int vars into an array 0 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