Why doesn't my editor script run?

So I wanted to make a simple sprite snapping script to make sprite placing easier.
I first made a Editor Window script so that the user can select the sprite size and if snapping should be enabled, which worked. But since the Update function a Editor Window class only runs when the actual window is open, I needed to make a second class which handles the actual snapping, and is of type “ExecuteInEditMode”.

So I made a script, did everything but it doesn’t seem to call the Update function in my script.


This is how the editor script looks like :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

[ExecuteInEditMode]
public class EditorTileAlign : MonoBehaviour
{

    private void Update()
    {
        Debug.Log("Ran");

        if (!Application.isPlaying && EditorTileAlignWindow.IsAlignerEnabled())
        {

            GameObject[] selected = Selection.gameObjects;
            for (int i = 0; i < selected.Length; i++)
            {
                if (EditorTileAlignWindow.GetTileSize().x != 0)
                    selected<em>.transform.position = new Vector3(Mathf.Round(selected<em>.transform.position.x * (1 / EditorTileAlignWindow.GetTileSize().x)) / (1 / EditorTileAlignWindow.GetTileSize().x), selected_.transform.position.y, selected*.transform.position.z);*_</em></em>

if (EditorTileAlignWindow.GetTileSize().y != 0)
selected.transform.position = new Vector3(selected.transform.position.x, Mathf.Round(selected_.transform.position.y * (1 / EditorTileAlignWindow.GetTileSize().y)) / (1 / EditorTileAlignWindow.GetTileSize().y), selected*.transform.position.z);*_

if (EditorTileAlignWindow.GetTileSize().z != 0)
selected.transform.position = new Vector3(selected.transform.position.x, selected.transform.position.y, Mathf.Round(selected_.transform.position.z * (1 / EditorTileAlignWindow.GetTileSize().z)) / (1 / EditorTileAlignWindow.GetTileSize().z));
}
}
}_

}


The line Debug.Log("Ran"); never actually prints out anything in the console, which is how I know that Update is never being called.
I do know that Update should only call when there’s a change in the scene, but I have tried moving my tiles in the scene view, and nothing prints to the console anyways.

This is not an editor script. This is actually a runtime script. All classes derived from MonoBehaviour are components, need to be attached to a gameobject and must not be inside an editor folder. “ExecuteInEditMode” does not turn a script into an editor script. It just makes the runtime script to run even you are not in playmode. It’s not ment for implementing editor functionality.

Editor scripts are either derived from “Editor” (if you want to create a custom inspector for a runtime component) or from EditorWindow (if you want to add a custom dockable window with your own functionality).

It’s also possible to not derive an editor class from anything and just use the InitializeOnLoad attribute and subscribe a static method to the editor update delegate. Example:

using UnityEngine;
using UnityEditor;

[InitializeOnLoad]
public class MyEditorScript
{
    static MyEditorScript() // this is the static constructor of the class
    {
        EditorApplication.update += OnUpdate;
    }
    private static void OnUpdate()
    {
        // do something.
    }
}

This is an editor script which must be located in an editor folder. “OnUpdate” will automatically run as soon as the project is loaded / reloaded

And this script is attached to a gameobject that is present and active in the scene hierarchy?

I think what you want to do is have a script for the tiles you want to move, then have an editor script derived from Editor for that object type which would have this logic.