Custom WindowEditor, but variable sets back to 0 when hit Play

I have lots of single GameObjects with a specific script. This script has an variable called id.
I’ve also made a custom WindowEditor that iterates through all selected GameObjects and gives them a distinct id.
It works fine, but every time I hit the Play button, all ids are set back to 0.

I’m not sure why, but I believe you actually need to use a SerializedObject and SerializedProperty in order to make communication between variables from editor to a normal script.

You probably need to do something along the lines of this in your EditorWindow script:

private SerializedObject idSerializedObject;
private SerializedProperty idSerializedProperty;

private void OnEnable()
{
    idSerializedObject = new SerializedObject(targetScript);
    idSerializedProperty = idSerializedObject.FindPropert("id");

    // In order to change the target variable you have a bunch of "value" properties in the SerializedProperty class you can use.
    idSerializedProperty.intValue = 999;

    // And then you have to apply the modified value to the object.
    idSerializedObject.ApplyModifiedProperties();
}