• 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 MapuHoB · Mar 03, 2015 at 01:00 PM · staticsingleton

In which cases I should use static?

I did some research and people always say there are some places that static is unavoidable or just the better choice but it most of the time it could be avoided. I have plenty of static vars & methods in my project so I got very suspicious. Simple examples when I'm using static are InputHandler.JustReleased(), CollidersManager.DeactivateColliders(GameObject go), SortingLayerManager.IsTopmost(GameObject go) and so on. I know I can just check if(Input.touches == 0) || Input.GetMouseButtonUp and so on but I like it more when it's all at 1 place.

Question: For what situations should I use static and for what I should definately avoid it? Consider singleton part of the question as well.

P.S. Don't give me the smalltalk like "when you want to have only 1 isntance" and bla bla just to get votes.

Comment
Add comment · Show 2
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 Owen-Reynolds · Mar 03, 2015 at 09:48 PM 1
Share

I agree with your first sentence: it's what I learned in computer school, and what I find works in practice: statics are a hack, to be used as a last resort. Which does not mean never. If you can look at your cat and say "using a static here is crud, but the other stuff I thought of is worse."

Statics were trendy here about a year(?) ago, and a lot of people used them like a garnish. It's like how you learn a new word and try to use it everywhere, even if it isn't really appropriate. Then there were a lot of "killing one enemy kills all" Qs, caused by using them.

avatar image MapuHoB · Mar 04, 2015 at 07:58 AM 0
Share

@Owen Reynolds , very nice explanations! Still curious though, where would you use (static && singleton) if use them at all?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Digital-Phantom · Mar 03, 2015 at 01:31 PM

I can give a couple of examples based on my own works. I use static for keeping scores, lives and health stats that I need to carry over from one scene to another (in conjunction with a game object and DoNotDestroyOnLoad game objects)

A use where I found static to be problematic was where I wanted cannons to fire just one cannon ball at a time. I used a static variable to keep track of 'ifCannonBallExists' which actually worked fine. UNTIL I placed more than one cannon in my scene and all the cannons then looked for this variable and only one would fire because they all thought the cannon ball existed.

So I guess any situation where more than one object needing the same information is definitely a time static is 'not' good.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Static List< GameObject> in Singleton 1 Answer

SetCursorPos reset when reloading scene 1 Answer

Remnant Empty Game Objects for Static Scripts 1 Answer

Variable Accessors in UnityScript (Get{} and Set{}) 1 Answer

Bug caused by Singleton Pattern 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