Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Paulius-Liekis · Sep 29, 2013 at 02:56 PM · exceptionquitondisableplay-mode

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...

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
10
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kerihobo · Nov 18, 2015 at 02:32 AM 0
Share

Hi @Jamora, I am a little stuck with this. How does one get this callback? $$anonymous$$y first time dealing with them.

Thanks, Regan

avatar image Bonfire-Boy · Nov 18, 2015 at 11:41 AM 3
Share

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).

avatar image Kerihobo Bonfire-Boy · Nov 18, 2015 at 07:33 PM 1
Share

OnApplicationQuit() worked perfectly for me :3

Thanks!!!!

avatar image
10

Answer by aka3eka · Apr 12, 2020 at 09:16 PM

I'm here in 2020. There are some changes in Unity 2019:

  1. 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).

  2. 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.");
                  }
              }
          }
    
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What to do when there is an error at runtime 1 Answer

Null Reference Exception 0 Answers

DexArchiveMergerException: Unable to merge dex 4 Answers

How to fail build in Unity 2017.4? 0 Answers

System.NotSupportedException: This protocol version is not supported 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges