BEHAVIORAL

Observer

What Is It?

Observer is a behavioral design pattern that allows other objects to be automatically notified when the state of an object changes.

There are usually two sides in this structure: the Subject and the Observer. The Subject is the object that holds the data or triggers the event. The Observer is the object that listens for that change and reacts when it happens. The Subject does not need to know exactly who is listening to it.

In Unity, this pattern is used quite often. C# events, Actions, delegates, and UnityEvents all work with the same basic logic. For example, when the player's health changes, the HealthUI can update the health bar, the SoundManager can play a damage sound, and the GameOver system can check whether the game should end.

When Is It Used?

The Observer pattern is especially useful when one event needs to affect multiple systems. It is a good choice when you do not know in advance how many objects will react to a change, or when that number may increase later.

In Unity, common use cases include player health, score, mana, quest progress, UI updates, sound effects, camera reactions, and general game events. Instead of writing all of this logic inside the Player class, each related system can listen for the event and handle its own responsibility.

It is also useful when you want to reduce dependencies between systems. Instead of one class directly referencing many other classes, it only publishes an event. However, if a simple direct method call is enough, using Observer may create unnecessary complexity.

Animation

Observer Pattern
0 listeners

Code

Assets / Scripts / Behavioral / ObserverDamageTester.cs
READ ONLY
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using UnityEngine;

namespace Patterns.Behavioral.Observer
{
    public class DamageTester : MonoBehaviour
    {
        [SerializeField] private PlayerHealth _playerHealth;

        private void Update()
        {
            if (Input.GetKeyDown(KeyCode.Space))
            {
                _playerHealth.TakeDamage(10);
            }

            if (Input.GetKeyDown(KeyCode.H))
            {
                _playerHealth.Heal(10);
            }
        }
    }
}

Advantages and Disadvantages

• Advantages:

  • It provides loose coupling between the subject and observers.
  • New observers can often be added without changing old code.
  • Each system can handle its own responsibility separately.

• Disadvantages:

  • Subscription management is important, especially in Unity.
  • Too many observers can make the event flow harder to follow.
  • Depending on listener order too much can create unstable logic.

Tips

When using Observer in Unity, the most important habit is managing subscriptions properly. In most cases, subscribing should be done inside OnEnable, and unsubscribing should be done inside OnDisable.

When choosing between UnityEvent and C# event, the purpose matters. UnityEvent is useful for Inspector-based setup, while C# events or Actions are usually lighter for frequent calls.

Be careful when using static events. Static events can keep old subscriptions after scene changes, so they should be cleaned manually if needed.

It is also safer to avoid subscribing with anonymous lambda expressions when you need to unsubscribe later. Using named method references is usually cleaner and safer in Unity.

In larger projects, simple logs can make event flow much easier to debug.