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 Vandarthul · Jan 22, 2015 at 08:00 PM · c#singletonlogic

Question About Singleton Pattern

Hello there,

I've come to a point that I started to think whether I am making a logical mistake here.

So I have a singleton class(named GameManager) and some public variables in it, lets say an Array of GameObjects. On my another script, I reach out my GameManager instance and directly use this Array of GameObjects as like reaching their child objects, using their information etc. Is it wrong to do that, performance-wise and mentality-wise? Somehow it feels wrong but I am not sure, thats why I'm asking here.

Thanks in advance.

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

2 Replies

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

Answer by DanSuperGP · Jan 22, 2015 at 08:20 PM

Singletons are an easy way to make a single globally accessible access point. There has been quite a bit of complex discussion about their upsides and downsides, and there are other more complicated patterns like dependency injection that are considered "better" by many people.

However, there's nothing fundamentally wrong with it as a technique performance wise, or at least nothing that's going to make a significant difference in the scheme of things. It's a much better choice than doing GameObject.Find(), which is terribly slow.

Mentality wise, it causes your code to be tightly coupled since it requires your other scripts to have knowledge of your Singleton, and the game objects that contained within your singleton. This can lead to your code becoming brittle and hard to maintain if you need to change or replace the singleton with other classes.

In your case, this is probably not a big deal, it's probably just fine to use a singleton. Dependency Injection might be a "better" solution from a computer science perspective, but it also requires a lot more setup and understanding that probably isn't necessary for what you're doing.

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 Vandarthul · Jan 22, 2015 at 08:32 PM 0
Share

Thank you for you comment and your suggestions. I've experienced aforementioned problem about changing the singleton with other classes but that's not a big deal for me at this time because the project I'm working on is not a big one and I can deal with changes since it compromises small amount of scripts. Thank you again for your fast reply.

avatar image
0

Answer by carrollh · Jan 22, 2015 at 08:29 PM

I totally agree with @DanSuperGP. But I'd like to add something that may be easier for you to actually do / is less daunting.

You probably don't need to have a Manager GameObject. You can totally make a static class that doesn't extend from MonoBehaviour. Static class work a lot like Singletons, except it's not ever instantiated. You already probably use other static methods, things like Lerp in the Mathf and Vector3 classes are examples. You can do this with variables as well. Works the same.

C# static member reference

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

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Singleton and DontDestroyOnLoad question 1 Answer

Singleton that can react to game events 2 Answers

Making a bubble level (not a game but work tool) 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