- Home /
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.
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.
A bit more fiddling I noticed that all of my awake methods are run twice. Once at initialization another when the application quits.
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...
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.
Yep, this was the issue. Woot for large group projects and not seeing the little things.
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.
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.
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
 
 
             Follow this Question
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
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                