- Home /
Singleton instance VS static variable ?
In Unity programming is it better to use a static variable or create a singleton instance to manipulate array of objects that will be always in memory and existing?
Answer by Fornoreason1000 · Jan 02, 2015 at 01:57 PM
Singleton patterns is used for object types IE i only ever want and need one instance of BattleMananger.
you can't really make a singleton array as far as I know. and can be really annoying to manage. if it wasn't an array I'd definitely say singletones
static variables on the other hand are little more easier but unlike singletons they can easily fall out of control. As for arrays you will not have an issue implementing it.
but singletons are usually better and safer. global variables are known to be "dangerous" while singletons not so much.
if you want you can make a wrapper class for your array and make a singleton of that.
class MySingleThatHasAnArray : Object {
public object[] myArray;
private static MySingleThatHasAnArray _instance;
private static bool applicationQuit = false;
public static MySingleThatHasAnArray instance
{
get
{
if (_instance == null)
{
_instance = new MySingleThatHasAnArray();
//Tell unity not to destroy this object when loading a new scene!
}
return _instance;
}
}
but have a read here. http://www.dotnetperls.com/singleton-static
you array is accesable via MySingleThatHasAnArray.instance.myArray :) for the most part singleton are better
Hope it help
Thnx for responding Fornoreason1000 but before looking for the singleton implementation, I'm looking for a benchmarking between singleton and static. I must before all make a decision between them :) That's why I should gather the + / - points for each of them.
check out the link i posted, it will do a better job than I did.
here what I know there is no"good and bad" it depends on what you want.
-Singletons can have interfaces, static classes cannot
-Singletons can use a factory pattern
-Singletons can have callbacks and Delegates
-Singletons are a single instance of a class, statics a global instance within a class.
-Singletons and statics are accessed globally
-Singletons leave the entire class global while statics only globalize the variables with the static keyword.
-Singletons are created when you decide to, statics are created when you reference them;
-Both are thread safe
-Singleton you can lazy load (load what you need), statics will always load everything
-A singleton is instantiated once, a class with static members can multiple times
Answer by Owen-Reynolds · Jan 02, 2015 at 05:37 PM
In practice, in Unity it's often easiest to put a monobehave script on an empty with a dontDestroyOnLoad. Which I suppose is technically a singleton.
Being able to see and edit Inspector vars is just so nice.
A lessor known thing you can do is make your $$anonymous$$anager a ScriptableObject.
remember there is a bug that can occur when you have $$anonymous$$onobehaviour singletons. (that is an actual singleton, not like Owen said).
basically you can reference the singleton after it gets destroyed, creating a ghost object, this ghost object is created during he destroy process thus doesn't get destroyed or unloaded. it just sits there. I don't know if its fixed or not, but its better safe than sorry.
but the common fix is to add a Boolean called application closing and set it to true in OnDestroy() then make instance return null when its true. (this maycause a null reference error, but it is to be expected and will only happen when the app closes)
I'm not sure if the same bug applies to ScriptableObjects since im pretty sure they get destroyed in the same process.
Your answer
Follow this Question
Related Questions
Saving last checkpoint hit when reloading scene - Singletons? 1 Answer
singleton, static var, local reference. Whats better? 1 Answer
Create a persistent GameObject using Singleton 3 Answers
Instantiating object from nothing 0 Answers
Class that extends another class that extends Singleton generic class 1 Answer