ExecuteInEditMode play break

I’ve run into a snag with the executeineditmode on my scripts. If I edit variables via code instead of directly in the inspector, when I press play they get reset and when I unplay, it goes back to how I set it in the code. E.g. if I have my code auto updating an integer based on where I put a gameobject, when I press play resets back to whatever I initialized it as, then returns to what my auto updater set it as when I unplay it.

Or I instantiate gameobjects and add them to a list (in edit mode) and when I press play, the list become length zero, removing the references to those gameobjects, but in this case the list doesn’t return to how it was, it remains length zero when I unplay.

How can I get my variables to stick without manually entering them?

I’m using C#, by the way.

Unity does not know you changed those values, so it doesn’t serialize them and loses the changes. You need to EditorUtility.SetDirty() somewhere on your modified stuff. See, for example, this answer. This is not limited to ExecuteInEditMode, but anything directly editing values bypassing inspector/editor dirty flag.