• 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
Question by warrocky · Mar 05, 2017 at 03:27 PM · c#unity 5unityeditoreditor-scripting

[ExecuteInEditMode] OnEnable Running twice on Play?

Hi. I have a GameObject with a simple script in C#, which has [ExecuteInEditMode] , however, when OnEnable runs, I want to be able to diferentiate if I'm in Play mode, or in the Editor. For which I am using Application.IsPlaying . However, when I click on Play, the OnEnable triggers twice, one with Application.IsPlaying being set to true, and another with it being set to false, this leads to some weird behavior. Is this expected? I don't remember having this problem previously. What would be the architecture guidelines to have one single call on each, at the appropriate times (Editor OnEnable, Play OnEnable).

Here is the script:

 #if UNITY_EDITOR
 using UnityEditor;
 #endif
 
 [ExecuteInEditMode]
 public class TestScript : MonoBehaviour {

     void OnEnable(){
         Debug.Log("On Enable");
 
         if (Application.isPlaying)
             PlayOnEnable();
         else
             EditorOnEnable();   
     }
 
     void PlayOnEnable(){
         Debug.Log("Play On Enable.");
     }
 
     void EditorOnEnable(){
         Debug.Log("Editor On Enable.");
     }
 }

When I enter Play mode in the editor, OnEnable is triggered twice, one for EditorOnEnable and another for PlayOnEnable, in this order.

Thank you in advance

Comment
arielsan

People who like this

1 Show 1
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 Adam-Mechtley · Mar 06, 2017 at 08:26 AM 0
Share

Hi! Could you please file a bug report and post the case number here? I'm going to soon be fixing something else related to ExecuteInEditMode, so I'd like to look at this at the same time.

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by quizcanners · Jul 31, 2017 at 04:57 PM

I had a similar issue:

    [ExecuteInEditMode]
 public class TestScript : MonoBehaviour {
 public int version = 0;
 private void OnDisable()
 {
     Debug.Log("Disabling Playing: "+Application.isPlaying+" version "+version);
     version++;
 }

 private void OnEnable()
 {
     Debug.Log("Enabling Playing: " + Application.isPlaying + " version " + version);
     version++;
 }

}

The Output:

 Enabling Playing: False version 0
 
 >> I click Play.
 
 Disabling Playing: False version 1
 Enabling Playing: False version 2
 Disabling Playing: False version 3
 Enabling Playing: True version 1

The solution to the problem I had was to use "EditorApplication.isPlayingOrWillChangePlaymode", it will return TRUE for those 4 calls after "I clicked Play" and FALSE for everything else.

 Playing == True  &&  EditorApplication.isPlayingOrWillChangePlaymode == True

to find the OnEnable I need.

I see little point in describing my problem, since it was a direct consequence of not knowing about additional pair of Enable/Disable calls. Can anyone explain where those 2 calls are coming from and what are they for?

Comment
Vapid-Linus

People who like this

1 Show 1 · 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 Andres-Fernandez · Jan 28, 2020 at 09:17 AM 0
Share

I'm facing some similar issue. Had a breakpoint inside an OnEnable function and got in an infinite loop of OnEnable calls while debugging. It seems like switching from editor to Visual Studio and back to editor makes the call to OnEnable function (similar to when it compiles the source code upon switching to Unity editor from the code editor). Not sure why that's happening, tho.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

9 People are following this question.

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

Related Questions

Unity3d and monodevelop c# scripting on ubuntu 16.04 IntelliSense problems 0 Answers

How can I Update c# version? 2 Answers

OnGUI call caused Cardboard Editor Scripts Errors 0 Answers

Double Array (Custom Editor) 1 Answer

Can't remove using Unity Editor, it is necessary for the development of the game. 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