Title: How to avoid "Some objects were not cleaned up when closing the scene. (Did you spawn new GameObjects from OnDestroy?)" · Issue #17 · UnityCommunity/UnitySingleton · GitHub
Open Graph Title: How to avoid "Some objects were not cleaned up when closing the scene. (Did you spawn new GameObjects from OnDestroy?)" · Issue #17 · UnityCommunity/UnitySingleton
X Title: How to avoid "Some objects were not cleaned up when closing the scene. (Did you spawn new GameObjects from OnDestroy?)" · Issue #17 · UnityCommunity/UnitySingleton
Description: I think it's needed to unsubscribe from events in OnDestroy to prevent memory leaks, such as: private void OnDestroy() { if (StatsManager.Instance != null) { StatsManager.Instance.OnStatsChanged -= OnStatsChanged; } } This works, except ...
Open Graph Description: I think it's needed to unsubscribe from events in OnDestroy to prevent memory leaks, such as: private void OnDestroy() { if (StatsManager.Instance != null) { StatsManager.Instance.OnStatsChanged -=...
X Description: I think it's needed to unsubscribe from events in OnDestroy to prevent memory leaks, such as: private void OnDestroy() { if (StatsManager.Instance != null) { StatsManager.Instance.OnStatsChange...
Opengraph URL: https://github.com/UnityCommunity/UnitySingleton/issues/17
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"How to avoid \"Some objects were not cleaned up when closing the scene. (Did you spawn new GameObjects from OnDestroy?)\"","articleBody":"I think it's needed to unsubscribe from events in `OnDestroy` to prevent memory leaks, such as:\n```cs\nprivate void OnDestroy()\n{\n if (StatsManager.Instance != null)\n {\n StatsManager.Instance.OnStatsChanged -= OnStatsChanged;\n }\n}\n```\n\nThis works, except for when exiting play mode, sometimes it will log:\n```\nSome objects were not cleaned up when closing the scene. (Did you spawn new GameObjects from OnDestroy?)\n```\n\nI believe it's because:\n- Unity destroys `StatsManager` first.\n- Unity next destroys the other GameObject that unsubcribes from `StatsManager` event.\n- When checking `StatsManager.Instance != null`, it causes `StatsManager` to respawn since `StatsManager.Instance` is null. (Internal Singleton behavior)\n\nWhat's the best way to avoid this issue?","author":{"url":"https://github.com/KamilDev","@type":"Person","name":"KamilDev"},"datePublished":"2025-02-28T02:56:17.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/17/UnitySingleton/issues/17"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:4f546580-a032-0a6c-88cb-3e362d4ed7bf |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B4BA:136256:1349C67:1BD0ED5:6A633CDE |
| html-safe-nonce | 4f4089fe18deb1f7d8455b68774d5ae2b368d988ec4a044abe10c0c106a4e9c6 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCNEJBOjEzNjI1NjoxMzQ5QzY3OjFCRDBFRDU6NkE2MzNDREUiLCJ2aXNpdG9yX2lkIjoiODQ3OTQwODA4MjA5MzU5MDMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | d6316581123e92c76937740bec0264df5de6e3b8feffe05c6f36ca5190076cab |
| hovercard-subject-tag | issue:2886065688 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/UnityCommunity/UnitySingleton/17/issue_layout |
| twitter:image | https://opengraph.githubassets.com/10583119668d5ae61ecb9640190be799760dda55c01d3d8428de7ccdc5119667/UnityCommunity/UnitySingleton/issues/17 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/10583119668d5ae61ecb9640190be799760dda55c01d3d8428de7ccdc5119667/UnityCommunity/UnitySingleton/issues/17 |
| og:image:alt | I think it's needed to unsubscribe from events in OnDestroy to prevent memory leaks, such as: private void OnDestroy() { if (StatsManager.Instance != null) { StatsManager.Instance.OnStatsChanged -=... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | KamilDev |
| hostname | github.com |
| expected-hostname | github.com |
| None | 59e55daad7174ca59d63c6974d58276ccb5477442e550bebb3c035e1bef11c94 |
| turbo-cache-control | no-preview |
| go-import | github.com/UnityCommunity/UnitySingleton git https://github.com/UnityCommunity/UnitySingleton.git |
| octolytics-dimension-user_id | 19302334 |
| octolytics-dimension-user_login | UnityCommunity |
| octolytics-dimension-repository_id | 103733805 |
| octolytics-dimension-repository_nwo | UnityCommunity/UnitySingleton |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 103733805 |
| octolytics-dimension-repository_network_root_nwo | UnityCommunity/UnitySingleton |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 990295d92a4cc7b63fbbd83a046217cd7d77d49c |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width