- Home /
singleton, static var, local reference. Whats better?
Hi I've spend last day researching on about this, but still have a question in regards to how to set up GameManagers.
I can probably understand why one would use a singleton over static variables, and if I get it correctly is the the very fact to avoid static variables themselves. On the other hand, I presonaly find it easier to read code when I make a private local variables on each script I want to read from the GameManager, so that later on in the script I code like for example m_playerScore = 5; instead of GameManager.instance.playerScore = 5;
Or for functions I prefer coding: m_gameManager.DoSomething(); instead of GameManager.instance.DoSomething();
Though obviously the fact that I prefer it doesn't mean anything if this is a bad practise. So my quetion here is, why not do this instead of having a singleton for referencing to a GameManager, SpawnManager, InventoryManager etc etc from other scipts that need them?
I've done my research and all but it didnt answered me this. Thanks
Edit--Javascript
Answer by ScroodgeM · Aug 25, 2012 at 01:41 PM
the main reason to use singletons instead of totally static class with static variables is cause unity serialize only instances and it's public properties that are marked as serializable. so, if your class should have one instance in project and needn't to store any external data/links using unity serializer, you can use static class
Hi thanks for your answer. But mostly my question was not about why use Singleton over Static variables, but rather if it's bad for some reason to use local references ins$$anonymous$$d of either singleton or static .Thanks
i don't know reason why it's bad to use local references. simply be aware with it, cause unity sometimes use structs that can't be referenced.
in unity optimization guide there are recommendations to use local references once linked at start for fast future use
http://docs.unity3d.com/Documentation/ScriptReference/index.Performance_Optimization.html
Thanks. for the link. Call me a noob or something (i am) i still don't know how to handle this :/ damn.
what exactly you can't handle? you can attach scripts to other scripts (instances-to-instances) for fast use in future, you can find existing by type or object name a store reference to script etc.
Your answer
Follow this Question
Related Questions
Setting Scroll View Width GUILayout 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Unity3d Javascript Reference Script Variable 1 Answer
I have a movement script, but how can i make it so i can move half the speed while in the air. 0 Answers