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?
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’.
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