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 /
This question was closed Mar 25, 2014 at 08:52 PM by Fattie for the following reason:

Duplicate Question

avatar image
1
Question by L1vD3v · Mar 25, 2014 at 06:06 PM · javascriptsingletonimplementationimplement

How to implement a singleton

I have made a script with a counter in it, and then to make this counter keep count through the whole game, I am using DontDestroyOnLoad (transform.gameObject);. There is just the problem, that when i start on the first scene nothing is wrong but, then when I have played through a level and is back on the main scene, the counter(which is displayed) has been duplicated. I saw something about implementing a singleton in another thread, but I didnt really understand how I would implement it into my script. Do I have to make a seperate script with the singleton in, and then connect it to the script with the counter or what would the best way of doing it be?

Script:

 static var energyObj : int = 5;
 
 var counter : float;
 
 function Awake () {
 DontDestroyOnLoad (transform.gameObject);
 }
 
 function Start()
 {
 counter = 1800;
 }
 
 function Update () {
  
  if (energyObj < 5) {
   counter -= Time.deltaTime;
      if(counter <= 0){
         energyObj += 1;
         counter = 1800;
         Debug.Log("Energy is " + energyObj); 
         }
      }
      
  energyObj = Mathf.Clamp(energyObj, 0, 5);
  
  if (energyObj > 5) {
          energyObj = 5;
     }
  if (energyObj < 0) {
          energyObj = 0;
      }
 }
 
 function OnGUI () {
 
 if (energyObj < 5) {
     var minutes = Mathf.Floor(counter / 60).ToString("00");
     var seconds = (counter % 60).ToString("00");
     GUI.Label(new Rect(100,40,400,40),"To Next: " + minutes + ":" + seconds);
     
     }
 
 }
Comment
Add comment · Show 4
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 Fattie · Mar 25, 2014 at 08:06 PM 0
Share

There's only one realistic way to have singleton-like concepts objects in Unity:

http://answers.unity3d.com/questions/663351/design-advice-power-up-system-static-variables.html

it has been the subject of incredible discussion on here. Note the many linked questions etc

 Grid.hopeItHelpsYou()  !  :)
avatar image Fattie · Mar 25, 2014 at 08:07 PM 0
Share

I would forget about singletons unless you're an advanced experienced programmer. Learn how to use trivial Static classes, which are very often the solution you want.

avatar image L1vD3v · Mar 25, 2014 at 08:24 PM 1
Share

I used the link, and it worked. Thank you so much!

avatar image Key_Less · Mar 25, 2014 at 08:52 PM 0
Share

Glad I could help, I've converted my comment to an answer.

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by Key_Less · Mar 25, 2014 at 08:00 PM

As an alternative to implementing a singleton class, you could simply destroy the duplicate when the scene is reloaded. There is a good reference on how to do this here.

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
avatar image
1

Answer by robertbu · Mar 25, 2014 at 07:04 PM

http://rockonflash.wordpress.com/2010/10/21/singletons-in-unity3d/

http://wiki.unity3d.com/index.php/Singleton

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

Follow this Question

Answers Answers and Comments

22 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Setting Scroll View Width GUILayout 1 Answer

Need help understanding scriptable objects. 1 Answer

Extending a Singleton base class (UJS) (?) 2 Answers

Variable Accessors in UnityScript (Get{} and Set{}) 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 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