Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 captaincheerios · Sep 13, 2011 at 12:33 AM · singletonawake

Awake Method Called Twice

I am working with a main script that calls and creates a singleton instance. However the issue I am having is that this awake method gets called multiple times.

The Awake method is being called at the start of the project once, however once I stop the game it runs the awake method one more time after the singleton is destroyed and creates a new one.

so i keep getting extra singleton instance, eg. Singleton is destroyed but at the very end it runs that Awake method a second time when the app quits this continuously generating more instances every time i test.

Comment
Add comment · Show 3
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 captaincheerios · Sep 12, 2011 at 11:59 PM 0
Share

Simply put Awake() { SingeltonInstance.Instance.Run$$anonymous$$ethod(); }

that awake method gets hit twice and I have no references in my code. I am pulling that method call out of the awake, but I would really like to know why the method, that Unity states is only called once, is getting called twice.

avatar image captaincheerios · Sep 13, 2011 at 12:05 AM 0
Share

A bit more fiddling I noticed that all of my awake methods are run twice. Once at initialization another when the application quits.

avatar image vxssmatty · Sep 13, 2011 at 12:56 AM 0
Share

Thats odd, i tried to replicate.. but obviously we all do this all the time and we'd of noticed if it were happening...

only 2 things i can think of... your script execution order in preferences... does it have an Awake at the end somehow? like at quit.

Or do you have a OnApplicationQuit() and calling your method somewhere?

that all i can think of...

1 Reply

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

Answer by Bunny83 · Sep 13, 2011 at 01:28 AM

My guess is that you used ExecuteInEditMode which forces tha script to run on "both sides", in run-mode and in edit-mode. Every transition from one mode into another the whole initialization-chain is executed. When you start the game Awake gets called in run-mode and when you stop the game you enter the edit-mode and Awake gets called again.

Comment
Add comment · Show 5 · 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 vxssmatty · Sep 13, 2011 at 01:38 AM 0
Share
  • for this idea - Good if others have this issue

avatar image captaincheerios · Sep 13, 2011 at 05:35 PM 0
Share

Yep, this was the issue. Woot for large group projects and not seeing the little things.

avatar image CyberMew · Jul 16, 2013 at 04:27 AM 0
Share

I had this same issue too. The thing is right after you stop the game in the editor, it doesn't release the resources. $$anonymous$$y singleton's Init() gets called twice. Thanks for the information. I thought there was a bug in the singleton code.

avatar image Bunny83 · Jul 16, 2013 at 08:16 AM 0
Share

Well, that's why i never use Awake to initialize my singletons ;)

I use always this helper class for $$anonymous$$onoBehaviour-singletons:

 // C#
 public class $$anonymous$$onoBehaviourSingleton< TSelfType > : $$anonymous$$onoBehaviour where TSelfType : $$anonymous$$onoBehaviour
 {
     private static TSelfType m_Instance = null;
     public static TSelfType Instance
     {
         get
         {
             if (m_Instance == null)
             {
                 m_Instance = (TSelfType)FindObjectOfType(typeof(TSelfType));
                 if (m_Instance == null)
                     m_Instance = (new GameObject(typeof(TSelfType).Name)).AddComponent<TSelfType>();
                 DontDestroyOnLoad(m_Instance.gameObject);
             }
             return m_Instance;
         }
     }
 }

Just derive your class from $$anonymous$$onoBehaviourSingleton like this:

 //C#
 public class SomeSingleton : $$anonymous$$onoBehaviourSingleton<SomeSingleton>
 {
     // your class stuff
 }

This implementation will search for an existing instance and if it doesn't exist it creates one.

avatar image hoekkii Bunny83 · Apr 15, 2016 at 07:27 PM 0
Share

You still can use awake to initialize singletons, just a little trick (I only copied the important bits of the code)

 public class Singleton : ComponentBase where T : ComponentBase
 {
     private static T m_instance;
     public static T Instance { get { return m_instance ?? CreateInstance(); } }
     public static bool Instantiated { get; private set; }
     private static T CreateInstance()
     {
         if (Destroyed) { return null; } // Check if for preventing memory leak
         if (m_instance == null && !Instantiated) // !Instantiated $$anonymous$$A$$anonymous$$ES SURE IT ISNT CALLED TWICE
         {
             // Find object part
             if (m_instance == null)
             {
                 // PREVENT CALLING TWICE
                 Instantiated = true;
 
                 // Create new Gameobject with the 
                 GameObject obj = new GameObject();
                 obj.AddComponent<T>();
                 obj.name = typeof(T).ToString() + " (singleton)";
                 
                 // Set the instance
                 m_instance = (T)FindObjectOfType(typeof(T));
                
                 // RESET VALUE IN CASE YOU WOULD LI$$anonymous$$E TO DO SO$$anonymous$$ETHING WITH IT
                 Instantiated = false;
             }
             DontDestroyOnLoad(m_instance.gameObject);
             m_instance.Initialize();
             Instantiated = true;
         }
 }

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

6 People are following this question.

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

Related Questions

'Awake' not called first after scripts are recompiled during execution 1 Answer

singleton becomes null? 0 Answers

Why my code seems skipped after singleton pattern ? 1 Answer

How to create a main menu with singleton pattern? 2 Answers

Saving last checkpoint hit when reloading scene - Singletons? 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