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
1
Question by pretender · Sep 02, 2010 at 04:24 PM · javascriptclasssingleton

javascript singleton

hi! i am trying to implement singleton class but i am totally confused.

i made a class that i want to use in my project it goes something like this (this is the simplified version):

class EControl{

  var state : boolean;

  //constructor
  function EControl(){
    state=false;
  }

  function getState() : boolean{
    return state;
  }

}

so i later on i realized that i need to have to access object of this class in more then one script and has to be the same object so the value that is set is the same. i was looking what is available in the forums and i found two resources. this one i dont understand http://www.unifycommunity.com/wiki/index.php?title=AManagerClass and this one is not working (i get "there is no instance") http://forum.unity3d.com/viewtopic.php?p=388784#388784

so i would appreciate if someone could explain how to make this work and how to use one object of this class in two separate scripts.

thank you!

Comment
Add comment
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

1 Reply

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

Answer by SteveFSP · Sep 02, 2010 at 04:57 PM

Here is a simple example with comments:

class EControl {

 // This is the singleton instance that is shared with everyone.
 private static var Instance : EControl;

 // Everything that needs to get the singleton
 // instance, calls this function to get it.
 public static function GetInstance() : EControl
 {
     if (Instance == null)
         // This is the first time the function was called.  Need to
         // construct the shared instance.
         Instance = new EControl();
     return Instance;
 }

  var state : boolean ;

  // Private constructor.  This is what forces the class
  // to be a singleton since the only way to construct it
  // is via GetInstance().
  // NOTE:  Private constructors are not normally allowed
  // in JavaScript.  But it is OK in Unity.
  private function EControl()
  {
     state=false;
  }

  function GetState() : boolean
  {
    return state;
  }

}

You get instances of the singleton as follows:

private var control : EControl;

function Start() { control = EControl.GetInstance(); Debug.Log(control.GetState()); // The next line would cause a compile time error due to // the constructor's protection level. // control = new EControl(); }

Comment
Add comment · Show 1 · 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
avatar image mangoblue · Mar 21, 2012 at 11:53 PM 0
Share

Still wish I understood the importance of Classes! (Yes, I'm a noob and trying to get my head around them)!

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

No one has followed this question yet.

Related Questions

Access Variable in a class from another script 4 Answers

How to Typecast JS Variables as C# Classes? 0 Answers

how to call class details from another class.? 0 Answers

Should I manually delete my custom class instances? 1 Answer

Multiple classes inside each other 1 Answer


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