- Home /
#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.
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)
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.
Well, yes there's Stand-alone build selected - I never change that. How is this related?
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.
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
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...)
it can allow you to place some code for debugging that won't go in the build, like some GUI stuffs.
I see. So there's no way I could execute something ONLY in edit mode?
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.
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...
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
Nope. $$anonymous$$akes no difference. Attributes apply to the declaration that follows them, irrespective of whitespace.
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
Your answer
Follow this Question
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