- Home /
How do I use a game object to store a singleton?
Hi,
I've just recently started to use NGUI and have simultaneously been trying to figure out how to save values across loading. When researching how to show/hide panel, I came across this thread discussing singleton scripts:
http://www.tasharen.com/forum/index.php?topic=458.0
I've created what I understand is a "singleton" script that I want to attach to a game object in my scene so that I can turn panels on/off with SetActive, but at the moment I can't successfully attach it to an object because it doesn't derive from MonoBehaviour.
Here is my singleton script (i've been trying to stick with javascript since I've found it tricky to make c# and js talk to each other):
#pragma strict
class UIManagerClass
{
private static var Instance : UIManagerClass = null;
var panelState : boolean;
public static function GetInstance() : UIManagerClass
{
if(Instance == null)
Instance = new UIManagerClass();
return Instance;
}
private function UIManagerClass ()
{
Debug.Log(panelState);
if (!panelState)
Debug.Log("panel off");
}
}
The private function UIManagerClass() is where I believe I'd add the SetActive(true/false) code, I think?
And this is my OnClick function called from a button:
function OnClick()
{
var uiControl : UIManagerClass = UIManagerClass.GetInstance();
uiControl.panelState = false;
uiControl.GetInstance();
}
This doesn't show any errors at the moment, but from my understanding I won't be able to reference the panel objects until I can attach this script to a game object in the scene, so my question is, how do I do that?
I've tried adding "extends MonoBehaviour" and it lets me attach the script to an object, but tells me I can't use "new" and can only use AddComponent(). However, I'm not sure how to go about this (whenever I try something I seem to get varying errors, so I'm sure I'm not using the right code protocol or something).
Any assistance anyone can offer is very much appreciated, and I apologise if my code and/or methods are awfully ugly; I've been trying to teach myself by scouring the net and frankensteining pieces of code together.
Hi, sorry, I've been quite busy but have been looking at the solutions and links you provided, thanks. I was planning to make a post and tick an answer once I've figured it all out, but at this stage the "pseudo-code" example seems to be what's working.
Answer by Fattie · Jun 08, 2013 at 10:04 AM
there is no really great solution. please read the many, many very long discussions on here about it
http://answers.unity3d.com/questions/430582/extending-a-singleton-base-class-ujs-.html
http://answers.unity3d.com/questions/17916/singletons-with-coroutines.html
We have an iconoclastic approach that is very simple and works for us. On that last question notice my answer "Purely for convenience:..."
IMO there is, philosophically, no way to get around the issue that monbehaviors/singletons/statics don't mix
Your answer
Follow this Question
Related Questions
ScriptableObject stored in MonoBehaviour lost on quit 1 Answer
MonoBehaviour Inheritance, Singletons and Generics 1 Answer
Persistence of ScriptableObjects between Scenes? 2 Answers
Circular Dependency Issue 0 Answers
How do I fix this error when trying to use the Watson SDK for Speech to Text? 0 Answers