Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by MaishyN · May 26, 2017 at 12:32 PM · programmingsingletondesign-patterns

Singleton design.

Hi all!

I know this question may be quite opinion based. Basically I am considering two options for global access scripts design.

  1. Make a singleton for every single instance manager object (e.g. in a single player rpg game - one singleton object for databases, one for inventory, one for UI, one for dialogues, etc.).

  2. Make one singleton object (let's call it GameManager) that would hold a reference to all other managers.

Is any of these options better than the other one? I'm aware that there is probably no significant difference, except for e.g. code readability. I'm asking this because I sometimes see people wondering "Am I using singletons too much?", "Is it bad?" etc.

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
4

Answer by landon912 · May 26, 2017 at 01:42 PM

Good question.

First, I'll just state my answer: Option 2 or a derivative of it.

Why?

While Unity(and game programming in general) encourages or even forces you to use practices that would be considered "bad" on the other side of the programming fence, it's still important that we try to hold good standards. Normally, we should suggest that a singleton not be used at all in this case:

  1. Their global nature can hide the dependencies within your codebase. Making an object global to avoid explicitly passing it along is considered a "code smell" in the application world.

  2. Singletons violate the Single Responsibility Principle.

  3. They encourage tight coupling among your codebase.

  4. They are difficult to mock for Unit Testing(which is already hard enough in game programming).

  5. They are inherently a product of lazy loading.

Lazy loading is bad because it puts off the decision to load the resource to another time. In this example, the decision to load the resource is made whenever you first use the resource and this decision can be made unknowingly to the programmer. This is the big problem with lazy loading. The initialization time of the resource can change quite frequently without the programmer even realizing it or considering the implications.

For this reason, an explicit initialization order is preferred. Of course, this could be done with the Singleton pattern by explicitly asking for a reference in some startup order, but in such case we would essentially be back to a convoluted option two.

While option two does not solve problem one or two(which are arguably the most important), it is definitely the better of the two.

While we would love to avoid those problems by using better coding patterns, in game development and especially in Unity it can be quite difficult to uphold those standards. Ideally, we would try to use no globals at all, but alas we are in no perfect world. Game programming often prefers and at times forces that we break practice. Therefore, I think option two is your best solution.

Comment
Add comment · Show 1 · 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 MaishyN · May 30, 2017 at 09:23 AM 0
Share

Thanks for your answer! In last few days I spent some time reading about game program$$anonymous$$g patterns and most people also discourage to use singletons.

And the more time I spend working on my game the more I realize that there is some conflict between Unity3d and "good coding practices", just as you said.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Several Questions about MVC pattern 0 Answers

Can a gameobject replace another gameobject including variables that referencing to that object? 0 Answers

Can I ensure all script executed within 1 frame? 1 Answer

singleton, static var, local reference. Whats better? 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