#Singletons

1 messages · Page 1 of 1 (latest)

lilac rampart
#

I'd avoid making a single "god object"

#

you'll wind up with a ton of random features glued together

#

Here's all of the singletons in my stealth game.

#

there is a "game manager"; its job is to signal when the game enters different phases

#

like dying

worn thistle
#

hmm okay

#

I will look into what a singleton is, and return :)

lilac rampart
#

having several components that do separate jobs all on one gameobject is fine

#

it doesn't really matter where these things live

#

they just have to exist somewhere

worn thistle
#

as I see it, a singleton is basically just a global object, and will destroy it self upon being created if another already exists, right?

lilac rampart
#

The second half is just a nice safeguard

#

here's my singleton class

#
using UnityEngine;

public abstract class Singleton<T> : MonoBehaviour where T : Singleton<T>
{
    private static T _instance;
    public static T Instance 
    {
        get
        {
            if (_instance == null)
            {
                _instance = FindObjectOfType<T>();
            }

            return _instance;
        }
    }
}
worn thistle
#

but what is the difference between a singleton and a so-called "god object" then?

lilac rampart
#

public class NoiseManager : Singleton<NoiseManager>

#

a "god object" is just something that's responsible for way too much

worn thistle
#

Isn't a singleton an object as well

#

?

lilac rampart
#

yes, everything is an object :p

#

it's the "god" part that's the problem

#

for example, my enemy class had 2,500 lines of code in it and handled WAY too many random things at once

#

i'm refactoring my game to split that up

worn thistle
#

okay

#

ive read some things

#

seen a video

#

So I think I know just enough about Singletons to say: It'll be something to worry about another time

#

As I've not spent more than 10 hours making Unity games yet, I think if it works, it works yk haha

#

Proper ways to go about doing something, will just come along the journey