Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by KyleB92 · Jun 08, 2013 at 10:01 AM · javascriptscriptableobjectmonobehaviournguisingleton

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KyleB92 · Jun 14, 2013 at 01:35 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges