How do I fire a method when selection is changed in the editor's hierarchy?

I want to have an editor script that can, for example, change the name of a gameobject that I select in the editor’s hierarchy. Everything I try doesn’t fire.

Did you try OnSelectionChange ?
It fires when an Object gets selected in the Editor View or in the Hierarchy. You can then retrieve information about the GameObject like Instance ID. And so on.

However. I think it would be an easier approach to have a field were you enter the new name and a Field you Drag&Drop the Object you want to rename in (using ObjectField). After renaming you can clear that Field so the next Object can be Dragged in there.

Regards Michael