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 dqhendricks · May 22, 2015 at 08:42 PM · singleton

Is there a known issue where singletons will run constructors multiple times?

I am seeing some strange behavior in a class I am working with. It is a singleton that looks like this:

 class GameState {
     
     static private var gameState : GameState = null;
     
     static function Instance () : GameState {
         if ( gameState == null ) {
             Debug.Log( 'test1' );
             gameState = new GameState();
         }
         return gameState;
     }
     
     function GameState () {
         Debug.Log( 'test2' );
         // do other stuff here

     }
 }

There's more, but I have cut it down to the necessary pieces. When I run GameState.Instance(), here is what prints:

 test2
 test2
 test1
 test2

So it appears what is happening is that the constructor is running twice before I ever even call new GameState(). Why would this occur??

I have ruled out other parts of the code calling new GameState() by doing a thorough search of the entire project's files. I'm at a loss as to why this would happen.

There are a couple of other classes contained within this same file. The file is named GameState. I am not sure if this is relevant to the issue.

Comment
Add comment · Show 2
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 Lo0NuhtiK · May 22, 2015 at 09:39 PM 0
Share

I just copy/pasted the code above into a new UnityScript, then made another one with nothing but :

 function Start()
 {
    GameState.Instance() ;
 }

... attached script 2 to three objects in the scene. Pressed play. Result.

test1
test2

Then added script 2 to one of those objects 4 times, and another one of them 3 times. Pressed play. Same Debug result.

Edit : ...Named the file GameState also just now, at first it was named something else. Same debug result, tho.

avatar image dqhendricks · May 22, 2015 at 09:48 PM 0
Share

I guess I should have tried that. Very interesting. It makes me think this has something to do with the other classes contained within the same file.

Even though the other classes have different names, if they are contained within a file named after the first class, maybe is causing something weird.

0 Replies

· Add your reply
  • Sort: 

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

20 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

Related Questions

Saving last checkpoint hit when reloading scene - Singletons? 1 Answer

singleton, static var, local reference. Whats better? 1 Answer

Create a persistent GameObject using Singleton 3 Answers

Text UI erases after new scene. 1 Answer

Class that extends another class that extends Singleton generic class 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