How to detect if Prefab was added to the scene in Edit Mode?

I’ve made a custom editor and want the Prefab which contains the custom editor script to run some code when it detects it has been added to the scene in Edit Mode. I only want it to run the code once when it’s added.
I can detect Edit Mode using:

if (!Application.isPlaying)

but haven’t been able to detect the adding-to-the-scene part.

Hi there,

You should look into ExecuteInEditMode.

That page has everything you need to know about running code while in the editor.


As for running it when a prefab is instantiated in the scene the simplest way I know of is gameObject.scene, which returns the scene with all of its properties. I would just check gameObject.scene.name to see if it’s in the scene or not.