Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
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
Add comment · 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 ExecuteInEdit$$anonymous$$ode, so I'd like to look at this at the same time.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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
Add comment · 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.

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

unity showing download file delay 0 Answers

How to use a boolean to control another object boolean using C# script 4 Answers

When Pressing a key to animate it is sometimes responsive sometimes it is not how can I fix this? PLEASE HELP! 3 Answers

Record a specific movement of a gameObject, then save it as an animation in Unity? 1 Answer

how to access the buttons when the AR camera is turned on. 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