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 rambramdt · Jun 13, 2014 at 08:22 PM · staticsingleton

Singleton vs. Static

I know this issue probably has been brought up many times, however, I am not sure my specific concerns were addressed very clearly.

I have been using Singletons a lot and it has made my life a lot easier. From what I've read, one of the major potential problems with singletons is that they may not be thread safe. For one thing, I am not doing multi-threading or web development; for another, the singleton script from Unity Wiki seems to be thread safe (it came with a lock). So, I stayed with it, despite of people's warning "the singleton pattern has been abused...", what exactly do they mean by that any way?

So I've singletoned. The Instance keyword is a little cumbersome, but the main thing is that I feel like an amateur. Not that I am not, but I want to be as professional as I can. When I look at professional packages, what is private, public, or static, is all very clear.

So, I decided to be professional and replace some of my singletons with static and I faced some major obstacles:

  1. Static attributes and properties are not exposed to the inspector. To get around it I had to use additional variables and assign the value using Awake().

  2. Data binding becomes very difficult. It is doable, but extra work is required.

So, the consequence was that I got a ton of extra variables and spent a lot of time deciding which ones are private, public, or static, all the while making sure that they can accomplish what I have already accomplished very easily with singletons.

This begs the question, is it worthy it? Does the convenience of singletons come with a price? Does it totally do away with Static? Should we embrace Singleton?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Jun 13, 2014 at 09:40 PM

The thread safeness is not the real concern about singletons. The actual dangerous / evil "part" of a singleton is it's "staticness". It makes no difference if you use static variables or one static variable which holds an instance of a class (which is a singleton). Both store data statically which introduces global state into your program.

You might want to read those:

  • What is global state

  • Why is global state evil

The problem with singetons / static variables is that if you write a component that uses those, it's behaviour becomes hard to test and sometimes leads to unpredictable results. Also if you want to reuse the component half a year later you usually don't remember what hidden dependencies the component has. It's not obvious since the access happens somewhere behind the scenes. For example: you just want to use a flashlight script you've written a long time ago and suddenly there's an instance of a player script which you don't use but the flashlight uses a player singleton behind the scene. That could generate huge chains of classes which are secretly linked together.

It also complicates debugging, makes it very difficult to turn the game into a multiplayer game and makes it almost impossible to test the class itself in a testing environment. In small projects that might not be such a big deal, but if the project gets larger you'll lose track of what's going on, where and when.

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 rambramdt · Jun 13, 2014 at 10:36 PM 0
Share

ok. I get it. So if I am using Static I might as well use Singleton.

I know I haven't been heeding the warning against globals, I have to really think about that. Thanks for the re$$anonymous$$der. I'll keep "Dependency Injection" in $$anonymous$$d.

avatar image rambramdt · Jun 14, 2014 at 03:00 AM 0
Share

I am very grateful for getting some answers and professional insights here.

One more question: does Toolbox avoid the pitfalls of Singleton?

http://wiki.unity3d.com/index.php/Toolbox

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Static singleton is (not nullref -empty-) but assigned to "null" in the other Scenes 2 Answers

Would setting this static cause a memory leak? 1 Answer

When to create singleton and static objects, 0 Answers

Data from a simple singleton / static class return null 1 Answer

Static reference cost 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