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 Fattie · Jan 16, 2012 at 09:56 AM · javascriptperformancevariablemonodevelopstatic

Trivial question for Javascript/Unityscript experts: static variables?

Attn Unityscript EXPERTS:

in normal computer languages, you might mark a variable "static" ...

 my happy high performance function ()
    {
    static my variable X
    do lots of processing, use X as much as you like
    }

so as to give a performance hint to a modern compiler that you do not want to go to the effort of instantiating the variable, or indeed resetting the variable, every time (or indeed ... you may algorithmically need the value to be maintained between calls, but here i'm interested in the performance aspect).

As far as I can see the tag "static" does not exist in Unityscript. Normally for a similar effect I just do this:

 private var _XitsMoreOrLessLikeAStatic : float;
 function Happyfunction ()
    {
    do lots of processing, use _XitsMoreOrLessLikeAStatic as much as you like
    }

(in fact interestingly that can improve performance slightly - some tests seemed to show so, anyway.)

Obviously that works fine but is simply a little untidy, you have all those variables hanging out in the script at hand.

I have absolutely no understanding, at all, of Javascript, Unityscript, the compilation ? / interpretation ? paradigms at work in the stack all the way between unityscript and the game running on an iPad ... so given that, can you tell me ..

.. is there a way to mark variables "static" in that sense? is it anyway (perhaps?) completely meaningless in the Unity milieu? have I just screwed-up something simple and "static" is spelled differently, or some such? Help!

Thanks!

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

4 Replies

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

Answer by Eric5h5 · Jan 16, 2012 at 01:17 PM

Static is spelled "static", but there is no point at all trying to use that for performance. The only reason you would use a static variable is if you have a global variable that should have one instance per class, rather than one instance per object.

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 Eric5h5 · Jan 16, 2012 at 02:09 PM 0
Share

There is no way to achieve the gain you discuss. Local variables typically perform slightly better than global variables. Also they make the code better, so always try to use local variables where possible.

avatar image Fattie · Jan 16, 2012 at 06:15 PM 0
Share

$$anonymous$$agnificent - thank you for your expertise. Cheers for now.

avatar image
3

Answer by MartinCA · Jan 16, 2012 at 02:36 PM

I think you're thinking in terms of native languages, and this is why you misunderstand the static behavior is Unity.

Unity uses C# and JavaScript, which are class-based languages and as such the program itself is a class, and every program component is also a class. As opposed to native languages where the execution entry point is a function, in C#/Java the execution entry point is a class.

That said, the definition of a static variable in such context, is a variable that exists OUTSIDE of a class instance, and exists only ONCE for every class definition. Consider this pseudo code:

 SomeClass
     public SomeVariable

 OtherClass
     SomeClass cls
     SomeFunction()
          cls.SomeVariable <---- ACCESSING AN INSTANCE OF SomeClass
 

In this case, using public variables, I would have to instantiate the class to create an instance of SomeVariable to access it.

Static variables and functions are exposed outside of the scope of the instantiated class, and only one such instance exists globally:

 SomeClass
     public static SomeVariable

 OtherClass
     SomeFunction()
          SomeClass.SomeVariable <----- ACCESSING A STATIC INSTANCE

As you can see, you do not need to create an instance of SomeClass to access it's static members.

As for optimizations, using the static keyword when appropriate surely boosts performance, however the considerations for optimizing high-level managed program are different from optimizing native programs.

You can read more on C# static keyword here

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
2

Answer by Statement · Jan 16, 2012 at 11:13 AM

Just add "static" before the declaration. You can't make local variables static (those declared in a function).

 static private var staticFloat : float;
 function Happyfunction ()
 {
     do lots of processing, use staticFloat as much as you like
 }
Comment
Add comment · Show 3 · 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 Fattie · Jan 16, 2012 at 01:33 PM 0
Share

Hi Statement! Thanks -- look, what difference could it make to add "static" to a variable declared out there in the file? (in a class?) I mean surely it is "static" anyway right ???? (How else could it be interpreted?)

avatar image Fattie · Jan 16, 2012 at 01:34 PM 0
Share

PS if this is a true fact: "You can't make local variables static (those declared in a function)." then you've answered the question, thanks. But I still don't see what "static" could mean in a file out in the general area outside functions - it's all static there.

avatar image aldonaletto · Jan 16, 2012 at 03:39 PM 1
Share

No, variable declared outside functions are member variables: they exist independently in every instance of the class. A static variable is unique, has program scope and program life time. It doesn't matter if you have instantiated several instances of the script where it's defined: the static variable is the same for all of them - and it exists even if you don't instantiate the script at all!

avatar image
2

Answer by Owen-Reynolds · Jan 16, 2012 at 05:37 PM

The confusion is that the static keyword has two completely different meanings, depending whether you use it in a function (which C# can't do) or a class.

The earliest, in C++ was what you are asking about -- it makes a local variable be persistant. Usually things like (not a great example): bool clickCounter() { static int clicks=0; clicks++; return (clicks>10); } Before static, we'd have to make clicks be a global, with a comment about how only clickCounter should use it.

Nowadays, we'd make a class called ClickCounter, member variable clicks and member functions Add and isDone. That would automatically make clicks be persistant and local.

A useful example of the other static (the one C#/UnityScript has) in a class:

 class String {
   static const int MAX_SIZE = 999; // all strings share this
   int size; // each string has its own size
    ...
 }

This tells readers that there is one MAX_SIZE for all strings -- you don't need to set it for each one. It also saves space, since each instance of a string doesn't need to store MAX_SIZE.

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

10 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

Related Questions

Static Variable Problem 1 Answer

Accessing another var on another script 1 Answer

variable = true from another script 2 Answers

Unknown identifier, OnCollisionEnter Error 1 Answer

Global variables - static keyword ? UnityScript? javascript? 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