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

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: Unity - Manual: Conditional Compilation

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.

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