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 FoxxCommand · Apr 01, 2014 at 03:23 PM · javascriptstaticstatic-variables

Storing variables temporarily during static functions?

I've heard that using static variables is bad practice, but I'm trying to make a static 'library' where in one function I set a variable, then in the second function I use that variable as a sort of constant and then that's all it'll do, what I have in mind is something like this

 //DoMathsGood.js
 static var constant : int
 
 static function setConstant(i : int)
 {
     constant = i;
 }
 
 static function doMaths(a : int, e : int)
 {
     return a*(constant+e)
 }

so then in some other script I can just go DoMathsGood.setContant(4) and whatever since "DoMathsGood" won't be attached to a GameObject which is why I'm making it all static, the reason for this is I'm building a File IO Library that I want to be able to call from any script in the game, so would what I have work in theory?

Comment
Add comment · Show 3
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 hamstar · Apr 02, 2014 at 06:44 AM 0
Share

I don't see why this wouldn't work. Why is using static variables bad practice? I would suggest that "constant" should be marked as private so it cannot be modified directly by other scripts.

avatar image Berenger · Apr 02, 2014 at 07:35 AM 0
Share

It's bad practice when it doesn't make sense. It does in that case, so go for it !

avatar image FoxxCommand · Apr 02, 2014 at 08:25 AM 0
Share

First, thanks for editing the code, my computer has a problem with the editing button for some reason. Second since it's not being attached to a GameObject would the 'constant' variable still be initialized?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by senc01a · Apr 02, 2014 at 07:50 AM

Using unprotected global and/or static variables is bad practice for various reasons, including racing conditions when using concurrently (two parts of your code want to access it at the same time) or making your code more coupled ( read more at Global Variables Are Bad.You probably want to encapsulate this in a Singleton, a commonly used pattern to deal with these situations. Singletons in Unity.

Having said that, your code will probably work, and if you do not have multiple threads, there is not a major reason why this might not work. Just keep in mind that the keyword static makes that variable unique across all of your program, so its value will always be shared no matter who calls it.

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 FoxxCommand · Apr 02, 2014 at 08:21 AM 0
Share

I really want to avoid using Singletons because from what I've read you have to attach it to a GameObject and I am really trying to make a library that you can just throw into the Plugins folder and start using that being said because it's not being attached to a GameObject will it still initialize the variable in the first place?

avatar image senc01a · Apr 02, 2014 at 08:38 AM 0
Share

I understand. And yes indeed, if you want to use a singleton you need to have a gameobject that this is attached to because of the way Unity is designed. In that case you may want to reconsider that particular piece of functionality that you are implementing. Since your $$anonymous$$ath utility has a state (which you call constant), then this should be an actual class/prototype that the caller can instantiate and object from, given a constant. That way multiple parts of the code can use it independently providing independent constants. This would make your code more independent and modular, and your would not be doing "nasty" things.

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

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

Related Questions

Is there any way to edit variables inside other scripts without declaring them as static? 1 Answer

how do I create a static Instance in javascript 2 Answers

Invalid IL code error when calling static function 1 Answer

Using static constants from an outside script 2 Answers

Setting Scroll View Width GUILayout 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