How do I use a game object to store a singleton?


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:

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 ()
 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;

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.

there is no really great solution. please read the many, many very long discussions on here about it

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