GENERIC SINGLETON

Unity

A generic singleton class, useful for managing game state

If you're unfamiliar with what a singleton is, see here: http://en.wikipedia.org/wiki/Singleton_pattern

This is a C# script snippet which you're sure to find a use for in almost any project. Whatever type you pass to T will become promoted to a singleton, allowing you to statically access it from anywhere.

As an example, say you had a class GameManager which has a member variable, points, which tracks the player's score. Normally, if you wanted to access that variable, you would need to pass an instance of GameManager and then pull the variable from it.

By using this class, you can easily access it like so:
Singleton<GameManager>.Instance.score


Or, if you wanted to query the GameManager object multiple times in succession, you could store it in a local variable and make repeated calls to it:
GameManager gm = Singleton<GameManager>.Instance;

int score = gm.score;

Posts

Pages: 1
I think the logic is okay here, but I feel it goes against some of the gains you can get from Unity and using the code as components for run-time accessibility.

Let's use the "GameManager" as an example. By attaching it like normal as a component on a GameObject you can use the editor features (linking existing instances or prefabs, ability to edit values in the GUI, etc) and still access it with a few more characters worth of code and not having to define it specially.

Let's say the GameObject you attach it to has a custom tag "GameManager" - you can:

GameManager gm = GameObject.FindGameObjectWithTag("GameManager").GetComponent<GameManager>();

Then you can reference it the same way:
int score = gm.score;

If you have some examples of where the Singleton method shines I'd be interested as maybe I'm not looking at it in the right light.
Sailerius
did someone say angels
3214
That's definitely true. I guess in this case I used a poor example. The way I usually use the singleton pattern is to manage a database. For example, in one of my games, I load and construct skills based on XML files, and there's no convenient way to manage that using a component (I want to load all the XML files in a folder, not have to drag and drop each one).
K-hos
whoa You guys are hi-chaining without me? That's just not right. :<
721
I don't think two lines really count as a script.
Not to mention if you know how to use a singleton effectively, then you likely already know how to make one.
And likewise if you know how to make one you probably know how to use it.

So this is pretty useless and seems like you're just trying to bump up your makerscore.
Sailerius
did someone say angels
3214
author=Khos
I don't think two lines really count as a script.
Not to mention if you know how to use a singleton effectively, then you likely already know how to make one.
And likewise if you know how to make one you probably know how to use it.

So this is pretty useless and seems like you're just trying to bump up your makerscore.

The fact that it's only two lines is what makes it so useful. The intuitive implementation of a singleton requires you to add boilerplate code to any class which you want to make into a singleton and requires significant refactoring if you ever choose to change your mind about it being a singleton. This allows you to make any class into a singleton without touching that class's code. I've personally found extensive use from this class and I posted it because it might save someone else time, too. :x
Awareness of this coding technique is also worhtwhile, so that's partly why I accepted it.
Pages: 1