Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 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
11
Question by vexe · Dec 28, 2013 at 08:53 AM · editorruntimeexecuteineditmode

#if UNITY_EDITOR stuff getting called at runtime as well?

I have this piece of code, that should only run in editor-time (right?) but yet it's also running at runtime.

 using UnityEngine;

 [ExecuteInEditMode]
 public class CheckForGoRename : MonoBehaviour
 {
     string previousName;
 #if UNITY_EDITOR
     void Update()
     {
         if (previousName != gameObject.name) {
             previousName = gameObject.name;
             print("GO new name: " + previousName);
         }
     }
 #endif
 }

I also tried: if (!Application.isEditor) return; - But it also got executed!

(I'd rather use the #if UNITY_EDITOR and wrap the whole Update function with it, as opposed to having it sit there using Application.isEditor - if I don't need it at runtime, why have it then?)

I want this code to be executed in editor-time only. Any idea why it's getting executed at runtime as well?

Thanks for any help.

Comment
Add comment · Show 5
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 fafase · Dec 28, 2013 at 09:00 AM 0
Share

you mean this code runs in the build?

avatar image vexe · Dec 28, 2013 at 09:02 AM 0
Share

I mean, I want this code to only run when I'm in the editor - editor mode - not 'play' mode (when play button is hit) (runtime) - but yet it's getting executed in both of them (edit-time and run-time)

avatar image fafase · Dec 28, 2013 at 09:05 AM 0
Share

In the build settings where you decide what build to be done, have you selected one, like web player? Then these lines should turn green in your script.

avatar image vexe · Dec 28, 2013 at 09:08 AM 0
Share

Well, yes there's Stand-alone build selected - I never change that. How is this related?

avatar image fafase · Dec 28, 2013 at 09:12 AM 0
Share

When you select one of them, I would guess it creates a #define, then that allows you to run as it would do in the build. As a result what is wrapped inside the corresponding turns into code and the ones wrapped in other if's are simply turned to comment.

3 Replies

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

Answer by asafsitner · Dec 28, 2013 at 09:11 AM

What you want instead of if (!Application.isEditor) return; is if (EditorApplication.isPlaying) return;

Besides that, the symbol UNITY_EDITOR will always return true when the running application is the editor - that's what it's asking - regardless of 'mode'.

You can define your own symbols as shown here: http://docs.unity3d.com/Documentation/Manual/PlatformDependentCompilation.html

Comment
Add comment · Show 5 · 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 vexe · Dec 28, 2013 at 09:20 AM 0
Share

Thanks. if (Application.isPlaying) return; also seems to work. But if the "#if UNITY_EDITOR" means running the code in both edit and play mode, what's its use then? (In either cases, I'd have to tag my script with ExecuteInEditor - I don't see how the #if is useful in this case...)

avatar image fafase · Dec 28, 2013 at 09:23 AM 1
Share

it can allow you to place some code for debugging that won't go in the build, like some GUI stuffs.

avatar image vexe · Dec 28, 2013 at 09:25 AM 0
Share

I see. So there's no way I could execute something ONLY in edit mode?

avatar image fafase · Dec 28, 2013 at 09:32 AM 0
Share

There is a similar thread here http://forum.unity3d.com/threads/69752-How-to-execute-a-script-ONLY-in-edit-mode yo may have seen it already.

avatar image vexe · Dec 28, 2013 at 09:38 AM 0
Share

Yes I read that. But I think there's something fundamental that I'm missing here. Using #if UNITY_EDITOR will actually run the code, only in the editor, by that it means both edit and play mode. The code inside the block, won't be included in the build - that's what I understood. I'm gonna try and build my game, and see if I get anything from inside those blocks...

avatar image
-1

Answer by ashworth · Sep 17, 2015 at 06:02 PM

I think the problem with your code was that you put the [ExecuteInEditMode] on a separate line to the void declaration, it should be on the same line as with the [HideInInspector] tag

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 Simon-O · Sep 24, 2015 at 08:28 PM 3
Share

Nope. $$anonymous$$akes no difference. Attributes apply to the declaration that follows them, irrespective of whitespace.

avatar image
0

Answer by qsp18 · Apr 08, 2021 at 10:36 AM

it refers to whether you start the game through Unity. If you build the game, then it will no longer be executed in the finished game. For example, it means the opposite of #if DEVELOPMENT_BUILD. Otherwise you can also use #if DEBUG and then it should only be executed when debugging

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

22 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 avatar image avatar image avatar image

Related Questions

Getting data from Editor into runtime 2 Answers

How does the particle emitter update in edit mode? 1 Answer

StreamWriter issue c# 0 Answers

Post Processing Profile not changing in build 0 Answers

Fastest way to get all components on GameObject? 2 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