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 ConfusedExpert · Feb 09, 2015 at 08:35 PM · beginnergridsingleton

Am I using too many Singletons?

I am making a Grid - Turn based game, something like Final Fantasy Tactics, Fire emblem, or Xcom. I just noticed, that I use a lot of singletons, and was wondering if I went a little overboard. Specifically:

  • My "Grid" script, which stores the matrix itself and provides methods for pathfinding and highlighting tiles, is singleton.

  • My "HudManager" script, which stores references to all of my GUI buttons, is also singleton. The idea is that the HudManager knows which player is currently selected, and will call methods referencing that script when a button is pressed.

  • I was thinking of making an observer class called MouseRayObserver. The idea behind is for it to be the only class that raycasts the mouseìs position, and inform its observers when the mouse button is pressed on an object. This class, in my mind, is also a singleton.

Am I going overboard with singletons? What logical thinking should I apply to decide if a class should be singleton? Thanks in advance

Comment
Add comment · Show 1
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 · Feb 09, 2015 at 09:38 PM 0
Share

This is just a design question. If you constantly have to remember which class has which variable, you used too many. If you are constantly scrolling through the giant drop down to find what you need, and aren't using 90% of them in most scripts, you didn't use enough.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Landern · Feb 09, 2015 at 08:54 PM

Are you using to many singletons??

Probably not, i guess the point is whats the difference between a singleton representing a singular existence of an instance of an object and ensuring that just ONE instance of a non-static object/class get created, other then patterns and wise design decisions, not a whole bunch. What you're doing with singletons can be done with instances of classes if you want.

Generally people using Unity don't quite get the order of operation or haven't grasped the ability to get references of GameObjects/MonoBehaviour/Component throughout the scene. This is generally why i think we see more FindGameObjectWithTag invocations instead of scoping down to the GameObject and caching the Component by type. But wouldn't it be easier to contain everything in on bucket?

Under those conditions they tend to seek out the one class/instance to rule them all and a bunch of static instances start existing to represent these classes and members.

On the surface it seems like there's some love story going on. You can invoke(get/set/whatever) a member from the static instance on a class(class-member) and get all the values you want, no GetComponent, nothing, it's dreamy at first.

Later in life you will start to understand that there should be limited uses for this implementation and it's actually preventing you from doing some pretty sexy object oriented fluffer-butter in your code. This is when you will decide to learn more about the engines you use and the languages they rely upon.

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

Answer by giulio-pierucci · Feb 09, 2015 at 09:01 PM

there is no real rule about the use of "singleton" classes .

At the end it's just a class that exposes a reference to his application , therefore , unless they face particular reasoning , should not lead to unpleasant consequences .

You must use the singleton when you need access to the class from many different classes , relieving you from the duty of to reference the instance , or search the instance with functions like FIND.

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 DanSuperGP · Feb 09, 2015 at 09:59 PM 0
Share

That is, until you learn the secret dark magic that is dependency injection.

avatar image giulio-pierucci · Feb 10, 2015 at 03:37 PM 0
Share

Oh, yes! Very interesting pattern. Seems powerful.

In witch context we may use it?

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

20 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

Related Questions

Instantiating Hexagonal Cube coordinate help 1 Answer

Detecting OnMouseDown when two objects are overlapping 0 Answers

Ground Plane when starting new project 2 Answers

beginner programming woe grids 1 Answer

Why won't my blocks fall? 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