Deactive ExceuteInEditMode for Methods of Inherited Classes

I have a MonoBehaviour class that requires to run an initialization script whenever a new object is instantiated in the Editor. This initialization script should be called only once for every new instance. I would go for:

public class BaseClass: MonoBehaviour
    protected virtual void Awake()

My problem is that several classes are derived from this class with various event functions that normally should not execute in EditMode. Also other developers should be able to derive new customized classes easily.

Any suggestions? Can something like Monobehaviour.runInEditMode help me to block script execution in Edit Mode again?

Well, it’s hard to tell without knowing what is done inside your Initialize method. Note that the editor is based on serialized data. So if you have an already “initialized” object and you duplicate it, it should be initialized already since it’s a duplicate and all serialized data is duplicated with it. It sound a bit like you work against the general workflow of Unity.

Don’t get me wrong I know there are some very specific cases like generating unique IDs for each instance. I’ve seen countless of attempts to solve this issue. However since the possibilities of how serialized data is handled are huge (now even more complex with nested prefabs / prefab variants / …), it’s almost impossible to cover all cases. I made an attempt myself some time ago which worked very well back then. Though I haven’t really checked it recently. Also UUID needs can vary heavily. Usually some should persist to identify a prefab but instances should not get a new ID since the ID should identify the prefab. On the other hand IDs may need to be unique across all object. On top of that comes the issue with custom object loading / saving.

So what exactly do you do in your Initialize method that is not carried over with a normal duplicate? Maybe you have to implement a ISerializationCallbackReceiver and make sure you handle the serialization of the relevant data yourself? Again, it’s hard to tell without knowing what this is actually about.