::Throughout this tutorial series, you may have noticed that sometimes when you stop Play mode, an error message shows up in the console. The error is something along the lines of Some objects were not cleaned up when closing the scene. This happens because Unity GameObjects can be destroyed in three ways: via code using the Destroy()
method, unloading a scene, or exiting play mode in the editor. The third is the culprit in this case.
To fix this error, simply add the following code snippet to the top of any OnDestroy()
method.
<code>if (!gameObject.scene.isLoaded)
{
return;
}</code>
This piece of code will simply check if the current scene is loaded when OnDestroy()
calls, ensuring that the function only runs when the current scene is loaded, and returns if it is not. This should eliminate the error message showing up.