I have the following pattern in a few of my objects - it makes a great fake singleton. But I am getting the error “Some objects were not cleaned up when closing the scene” when I stop playing the scene when I use this pattern. Anyone know how I can clean up that error? It’s getting very annoying.
protected static UnitsManagerScript sUnitsManagerScript;
public static UnitsManagerScript SceneInstance
{
get
{
if(!sUnitsManagerScript)
{
GameObject pGameObj = GameObject.Find("UnitsManager");
if(!pGameObj)
pGameObj = new GameObject("UnitsManager");
sUnitsManagerScript = pGameObj.AddComponent<UnitsManagerScript>();
}
return sUnitsManagerScript;
}
}
“UnitsManager” is a game object that exists in your scene. You’re adding a UnitsManagerScript component to it then holding on to a reference to that component in your sUnitsManagerScript variable. What this means is that your UnitsManager game object cannot be completely destroyed and garbage collected because it references the UnitsManagerScript which itself can’t be destroyed because it is being referenced by your UnitsManagerScript. Add a method like this:
public static void DestroyUnitsManager()
{
sUnitsManagerScript = null;
}
Try calling that method in the OnDestroy on a script attached to your UnitsManager object (like in your UnitsManagerScript). That should make sure your references are released and the object can be completely destroyed.
Hmm… have you set a breakpoint and verified that OnDestroy is even firing? So… UnitsManagerScript inherits MonoBehavior…
You should also try removing the component from the UnitsManager object. But looking at it here’s your biggest issue:
You have: UnitsManagerScript : MonoBehavior
Now, UnitsManagerScript has a static property… sUnitsManagerScript. You’re creating an instance of UnitsManagerScript (when you call AddComponent) and then you’re assigning it as a reference to it’s own property (if that makes sense)… except since it’s a static property it’s really part of the class definition and not the instance. I know there is another thread floating around about using a Singleton-ish Monobehavior… and there are some gotchas that were solved there.
I was getting this error when I quit the application in the unity editor (and probably on builds; you know how that goes…) and I figured out why it’s happening.
When you quit the unity application, script order matters a lot (you can change the execution order of scripts by selecting a script in your project panel > select “execution order” at the top right of the inspector window once the script is selected > then clicking the “+” button to add it to the bottom of the list or wherever it needs to be.
In other words: It is possible you are UNSUBSCRIBING to a delegate on a script that runs AFTER unity destroys the reference once the application quits.
To avoid this error, complete the steps above OR simply go to your “OnDisable” function, or wherever you are unsubscribing from a delegate/function (if it’s happening when quitting the game view, then it’s likely ondisable or something like it), and check if the instance is null.