Variables modified on other scripts through a Editor Script reset on Play?

First time dabbling with editor customization. I have a simple script that adds a Tools dropdown item to the top toolbar of Unity, and in this script I have a menu item that should

  1. Find an ID script on the selected
    transforms in the scene
  2. Change the ID on each script to a flowing int number (so our level designer doesn’t have to change them by hand on 20+ items)

Here’s the part of the script that does this:

public class MenuItemTools {
    	[MenuItem("Tools/Change scrap IDs #&c")]
    	private static void ChangeScrapIDs() {
    		Transform[] transforms = Selection.transforms;
    		foreach (Transform t in transforms){
    			s_scrapController sc = t.GetComponent<s_scrapController>();
    			if (sc != null) {
    				sc.scrapID = flowingScrapID;
    				flowingScrapID++;
    			}
    		}
    	}
}

The field I need changed is just an int field in the script found on the transform

	public int scrapID;

Everything works fine until I press Play and all the modified IDs reset to their originally set values. I’m guessing this has something to do with serialization and how the Play button and transition from Scene to Game work, but I’ve got no idea what I should be looking for here.

Searched for a while, found answers with suggestions to SerializableField and EditorUtility.SetDirty() but sadly these did not work.

Try set dirty for whole gameObject.

if (sc != null) {
                 sc.scrapID = flowingScrapID;
                 flowingScrapID++;
                 EditorUtility.SetDirty(gameObject);
             }