GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — How to avoid "Some objects were not cleaned up when closing the scene. (Did you spawn new GameObjects from OnDestroy?)" · Issue #17 · UnityCommunity/UnitySingleton

How to avoid "Some objects were not cleaned up when closing the scene. (Did you spawn new GameObjects from OnDestroy?)" #17

New issue
Copy link
New issue
Copy link
Open
Bug
Open
How to avoid "Some objects were not cleaned up when closing the scene. (Did you spawn new GameObjects from OnDestroy?)"#17
Bug
Copy link
Assignees
hasanbayatme
Labels
bug

Description

@KamilDev
KamilDev
opened on Feb 28, 2025
Issue body actions

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 for when exiting play mode, sometimes it will log:

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

I believe it's because:

  • Unity destroys StatsManager first.
  • Unity next destroys the other GameObject that unsubcribes from StatsManager event.
  • When checking StatsManager.Instance != null, it causes StatsManager to respawn since StatsManager.Instance is null. (Internal Singleton behavior)

What's the best way to avoid this issue?

Activity

  1. hasanbayatme commented on Feb 28, 2025

    @hasanbayatme
    hasanbayatme
    on Feb 28, 2025
    Member
    More actions

    I think a simple way to avoid this is to have a method to check whether there is any existing instance there without calling the get accessor on the Singleton class that initializes a new instance if there aren't any.

    You could try it in your local.copy and see how it performs, like add a new method/property that just checks the internal instance field
    of the singleton for null, and returns:

    public bool HasInstance => instance != null;

    This way, when you're unsubscribing for an event if the object is already destroyed, then no new object gets initialized by the singleton, as you're using the HasInstance in place of Instance in OnDestroy.

    This should be only intended to be used in such cases as of OnDestroy or OnDisable where the instance it self could be destroyed due to Unity unloading the scene.

    Reacted by Alexandre D. Roberge
  2. hasanbayatme
    added
    bug
    on Feb 28, 2025
  3. hasanbayatme
    self-assigned this
    on Feb 28, 2025
  4. hasanbayatme
    added the
    Bug
    issue type on Feb 28, 2025
  5. my-dimons
    added a commit that references this issue on May 22, 2026

    Add "HasInstance" boolean

    e0aecb7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @hasanbayatme
    hasanbayatme

Labels

bug

Type

Bug

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.