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 LK84 · Nov 22, 2016 at 12:23 PM · errorerror messagespawning problemsondestroy

Some objects were not cleaned up when closing the scene

Hi,

I get the following error when I exit my application in the Editor: Some objects were not cleaned up when closing the scene. (Did you spawn new GameObjects from OnDestroy?) I know there have been similar questions asked before but they all had an additional Error Message pointing to some line code with an OnDestroy() Method. My Problem, the Error Message is not pointing to any code (s. picture). Plus I dont have a single OnDestroy() Method in all my scripts!!! I know it's probably impossible to solve for you without seeing my code, but I only need a hint where to look, because right now I have no idea where to start looking for the origin of the error. alt text

EDIT: I dived a little bit deeper. Stack trace logging gives the following output:

Some objects were not cleaned up when closing the scene. (Did you spawn new GameObjects from OnDestroy?) 0x0000000140E4D6ED (Unity) StackWalker::GetCurrentCallstack 0x0000000140E4F411 (Unity) StackWalker::ShowCallstack 0x00000001406021D3 (Unity) GetStacktrace 0x00000001405FF59D (Unity) DebugStringToFile 0x00000001405FF9FC (Unity) DebugStringToFile 0x000000014046A277 (Unity) ValidateNoSceneObjectsAreLoaded 0x0000000140B73CA2 (Unity) EditorSceneManager::RestoreSceneBackups 0x0000000140BE2847 (Unity) Application::ExitPlayMode 0x0000000140BE2C3B (Unity) Application::SetIsPlaying 0x0000000140BE3716 (Unity) Application::TickTimer 0x0000000140E47E2E (Unity) FindMonoBinaryToUse 0x0000000140E49461 (Unity) WinMain 0x00000001415AD460 (Unity) strnlen 0x00007FFEB7E98364 (KERNEL32) BaseThreadInitThunk

In the Editor.log I found the following outout:

Some objects were not cleaned up when closing the scene. (Did you spawn new GameObjects from OnDestroy?)

"(Filename: C:/buildslave/unity/build/Runtime/Misc/SaveAndLoadHelper.cpp Line: 595)"

I got no idea about how to deal with the SaveAndLoadHelper.cpp. I can't even find it under the given path

ondestroyerror.jpg (29.0 kB)
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 Mister-Mortal · Nov 22, 2016 at 02:52 PM 1
Share

Are you using any plugins? If so, plugin could be the one causing problems.

avatar image LK84 Mister-Mortal · Nov 22, 2016 at 08:03 PM 0
Share

only AdColony. I'll look into it. Thanks for the hint

avatar image hexagonius · Nov 22, 2016 at 08:23 PM 1
Share

it's a common problem when using lazy singletons, those, that instantiate themselves when called and not in existence

2 Replies

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

Answer by LK84 · Nov 23, 2016 at 03:22 PM

Thanks to @hexagonius and @Mister-Mortal hints I found the solution. The error was indeed caused by a singleton. I am using a game Manager implementing the Singleton Pattern. It used to look like this:

         public class gameManager:MonoBehaviour
         {
             private static gameManager _instance;
             public static gameManager Instance
             {
                 get
                 {
                     if (_instance == null)
                     {
                         GameObject go = new GameObject("gameManager");
                         go.AddComponent<gameManager>();
                         DontDestroyOnLoad(go);
                     }
         
                     return _instance;
                 }
             }
         public event VideoEvent OnVideoFinished;
         public event VideoEvent OnVideoStarted;
         Awake()
         {
         _instance=this;
         AdColony.OnVideoFinished = IsVideoFinished;
         AdColony.OnVideoStarted = VideoStarted;
         .....
         }
     private void IsVideoFinished(bool adShown)
     {
         if(OnVideoFinished!=null)
         OnVideoFinished(this, new GenericEventArgs<bool>(adShown));
     }
 
     private void VideoStarted()
     {
         if (OnVideoStarted != null)
         {
             OnVideoStarted(this, EventArgs.Empty);
         }
     }
         }

As you can see there are two events sources in the gameManager class for AdColony videos. The delegate is defined as followed: public delegate void VideoEvent (object sender, EventArgs e);

I have Event Listener to those events at several points in my game: They are implemented like this:

 void Start()
 {
 gameManager.Instance.OnVideoFinished += LocalMethod;
 }
 void OnDisable()
 {
 gameManager.Instance.OnVideoFinished -= LocalMethod;
 }
 LocalMethod(object sender, EventArgs e)
 {
 //Do Something
 }

The error is caused for the following reason: When I quit the application objects gets destroyed in a random order. So If the gameManager Instance gets destroyed before any of the OnDisable() Methods, a new Instance of the gameManager class is created after the application has exited causing the error!! To solve the error I implemented the following modifications:

In the gameManager class (according to http://wiki.unity3d.com/index.php/Singleton):

     private static object _lock = new object();
     private static gameManager _instance;
     public static gameManager Instance
     {
         get
         {
             if (applicationIsQuitting)
             {
                 return null;
             }
 
             lock (_lock)
             {
                 if (_instance == null)
                 {
                     GameObject go = new GameObject("gameManager");
                     go.AddComponent<gameManager>();
                     DontDestroyOnLoad(go);
                 }
             }
 
             return _instance;
         }
     }
 private static bool applicationIsQuitting = false;
 public void OnDestroy()
     {
         Debug.Log("Gets destroyed");
         applicationIsQuitting = true;
     }

In the class containing the Event Listener:

     void OnDisable()
     {
         if (gameManager.Instance != null)
             gameManager.Instance.OnVideoFinished -= OnVideoFinished;
     }

Now if the gameManager instance is already destroyed a new instance can't be created

Comment
Add comment · Show 3 · 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 Schneider21 · Dec 19, 2018 at 03:51 PM 0
Share

This did the trick for me. Thanks for sharing your solution!

avatar image Sylker · Apr 17, 2019 at 05:35 PM 2
Share

I found out a better way to know if the application is quitting:

     [RuntimeInitializeOnLoad$$anonymous$$ethod]
     static void RunOnStart()
     {
         Application.quitting += () => applicationIsQuitting = true;
     }

The rest of the code worked great for me.

avatar image glenneroo Sylker · Dec 30, 2020 at 02:58 AM 0
Share

Thanks @Sylker ! For anybody else who can't get it to compile with "[RuntimeInitializeOnLoad$$anonymous$$ethod]", try this instead: [RuntimeInitializeOnLoad$$anonymous$$ethod]

avatar image
0

Answer by f3publishing · Aug 14, 2018 at 01:32 PM

There is something missing. Add _instance = before your AddComponent line.

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

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

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

Related Questions

Visual Studio Code doesn't work for with unity 2 Answers

What is happening and what does this MEAN!?!?!?!?!?!?!?!? AHHHHH!!!!,WHAT IS HAPPENING?!?! 1 Answer

Too many errors in the IDE (even for a fresh project), but editor doesn't throw any errors! 1 Answer

Fatal error! System out of memory! Unity 3D 1 Answer

Cannot import videos 0 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