Reflecting changes to a GameObject in the editor based on attached components

I have components which change the state of a GameObject when enabled/disabled.

For example:

  • I attach a Frozen component to a GameObject
  • When the component is enabled it will change the sprite of the GameObject to which it is attached to a frozen variant (the component also handles all the logic of the GameObject in a Frozen state)
  • When the component is disabled the GameObject will revert back to a standard state and its sprite revert also

I would like these changes reflected in the Editor if possible. I read about Unity - Scripting API: ExecuteAlways which may help here but it should only be applied to certain methods in the script OnEnable/OnDisable (where the sprites are set) and not all of it.

I’m also open to changing my approach as I’m not entirely happy with changing the sprite on a component basis. The art style does not lend itself to a layered approach (drawing the frozen sprite from the Frozen Component, when enabled, on top of the standard GameObject sprite) as each state is represented with a unique sprite. The issue with a unique sprite per state is that I would have to decide which state would have precedence when multiple states (Components are enabled).

Would appreciate any thoughts, advice, or pointing in the direction of documents in this area.

ExecuteAlways is all-or-nothing, you cannot have only some methods called always and others not. As the example in the documentation shows, you want all methods that are called by Unity to check with Application.IsPlaying to decide what they should do. You should also familiarize yourself with editor scripting in general, a lot of things work differently and you e.g. want to use Undo before making any changes in edit mode.

Another approach would be to use MonoBehaviour.OnDrawGizmos, i.e. instead of modifying the scene, you add some gizmo markers/icons that indicate the state of objects. These methods are always also called in edit mode, so ExecuteAlways is not needed.