Avoiding Singleton Patterns

Hi, first post on this forum ^^

I was watching the course on Unity Design Patterns and came upon the lesson that describes why singleton patterns are not always the best thing to use in code, and I want to try and phase them out of my programming. Problem is, I seem to have dug myself into a mental hole with how to get around it.

I mainly use monobehavior singletons to keep track of things that a lot of scripts may have a use for. Like, say, AudioController.Instance.PlaySound() or telling UI to update without needing a bunch of scripts to have access to the text object. I know I just need to think outside the box, but I may need some help cutting a hole in said box. Any advice?

First, there’s nothing wrong with using a Singleton.

For instance, script called GameManager is typically going to cause issues if you don’t use a singleton pattern. Of course there are other, more complicated ways around it, but simple is often better than complex. That being said, the Singleton pattern is often a go to with many developers, which in itself is fine. Where you can come undone is where one singleton references another singleton and vice versa. It gets even worse the more singletons added to that rotation.

A singleton is a tool in your toolbox. Nothing more, nothing less. So long as you don’t use a screwdriver when a hammer would be of better choice, you’ll be fine. You’ll add more and more tools to your toolbox over time and you’ll find you’ll use Singletons less over time.

I wouldn’t worry about it too much for now.

2 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms