- Home /
Is there a way to know that Unity is quiting Play mode?
My scripts work just fine, but when I unpress Play more button in Editor I get a bunch of these errors:
MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it.
It comes from OnDisable method, so I assume that Unity has destroyed some object before the call to OnDisable of my script.
Is there a way to know that Unity is quitting Play mode? or workaround it in some other way...
Answer by Jamora · Sep 29, 2013 at 03:43 PM
You can get a callback for when the user presses the Play button (pause too) by registering to EditorApplication.playmodeStateChanged.
using UnityEngine;
using UnityEditor;
[InitializeOnLoad]
public static class PlayStateNotifier
{
static PlayStateNotifier()
{
EditorApplication.playmodeStateChanged += ModeChanged;
}
static void ModeChanged ()
{
if (!EditorApplication.isPlayingOrWillChangePlaymode &&
EditorApplication.isPlaying )
{
Debug.Log("Exiting playmode.");
}
}
}
Alternatively you could just have a null check in your OnDisable before trying to access any other GameObjects.
Hi @Jamora, I am a little stuck with this. How does one get this callback? $$anonymous$$y first time dealing with them.
Thanks, Regan
The EditorApplication.play$$anonymous$$odeState solution only works in the Editor. While that does answer the question asked, it seems to me that they'll be getting those missing exceptions in builds too. So actually, the null check solution would be better (and simpler).
Another technique that would work in both Editor and builds is to use OnApplicationQuit() to set a "quitting" flag (that's initialised to false), and use that in OnDestroy/Disable (ie only do stuff that requires objects to still exist, if it's not quitting).
OnApplicationQuit() worked perfectly for me :3
Thanks!!!!
Answer by aka3eka · Apr 12, 2020 at 09:16 PM
I'm here in 2020. There are some changes in Unity 2019:
EditorApplication.playmodeStateChanged is now obsolete and was renamed to EditorApplication.playModeStateChanged (capital "M") and the event now has 1 parameter - PlayModeStateChange (see details here: https://docs.unity3d.com/ScriptReference/PlayModeStateChange.html).
Use the following updated code:
using UnityEngine; using UnityEditor; [InitializeOnLoad] public static class PlayStateNotifier { static PlayStateNotifier() { EditorApplication.playModeStateChanged += ModeChanged; } static void ModeChanged(PlayModeStateChange playModeState) { if (playModeState == PlayModeStateChange.EnteredEditMode) { Debug.Log("Entered Edit mode."); } } }
Your answer
![](https://koobas.hobune.stream/wayback/20220613115019im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
What to do when there is an error at runtime 1 Answer
I Have Another Question? 1 Answer
Published project quits unexpectedly? 0 Answers
Application.Quit needs to be called twice? 0 Answers
Unity 4.7.2 unexpectedly quits on opening in Mac OS 10.6 0 Answers