Modify GameObject Component values from custom EditorWindow

I’ve created a custom window with a range of buttons. When a game objects are selected, and a button is pressed, the myPrefab value of the MyScript component of the selected game objects are set to a particular prefab (according to which button is pressed).

This is working up until the point where I press play. I may have 10 game objects with the MyScript component, and using the buttons set each of them to contain a particular prefab. In the inspector, I can see the prefab value has updated for each of the game objects.

However, as soon as I press the play button, any modifications made by the buttons is undone.

To clarify, if I set the prefab in the inspector, then with a button, it will revert to the one set in the inspector. If I simply set it by the inspector, it won’t revert. If it is by default null, and only set by the buttons, it will revert to null. (Only the button results are being reverted).

Could anyone work out why this is the case? Is there some sort of confirm or save method I should be calling to “lock in” my choice?

Edit: Some code as requested

if (GUI.Button(rectangle, GUIContent.none))
{
	foreach (GameObject go in Selection.gameObjects)
	{
		MyScript component = go.GetComponent<MyScript>();
		
		if (component == null)
			continue;
		
		component.myPrefab = firstPrefab;
	}
}

Simply add “EditorUtility.SetDirty(component);” after the value is modified.
This marks the object as needing to be stored (instead of simply being the value shown in the editor).

In the inspector, things are marked as dirty as soon as you change them. Changing them through other editors like this however, means that the the object must manually be marked as dirty.

if (GUI.Button(rectangle, GUIContent.none))
{
	foreach (GameObject go in Selection.gameObjects)
	{
		MyScript component = go.GetComponent<MyScript>();
		
		
		if (component == null)
			continue;
		
		component.myPrefab = firstPrefab;
		EditorUtility.SetDirty(component);
	}
}