how-to-use-unity-events-and-delegates

How to Use Unity Events and Delegates for Cleaner Code

Introduction

Tired of messy if statements and hard-coded references? Unity Events and Delegates are the cleanest way to decouple your code, reduce dependencies, and build a modular architecture that scales.

In this guide, you’ll learn:

  • What Unity Events and C# Delegates are
  • How to use them to communicate between scripts
  • The difference between built-in UnityEvents vs custom delegates
  • Practical real-world examples (like UI or player health)

Let’s clean up your game logic like a pro.

What Are Delegates in Unity?

A delegate is a type that holds a reference to a method. It lets one script call another script’s function indirectly, without knowing anything about it.

Simple delegate declaration:

public delegate void OnPlayerDied();
public OnPlayerDied onPlayerDied;

You can then trigger it like:

if (onPlayerDied != null)
    onPlayerDied();

Or shorter:

onPlayerDied?.Invoke();

✅ This is perfect for saying:
“When something happens… let whoever is listening respond to it.”

What Are UnityEvents?

UnityEvents are Unity’s built-in event system — perfect for triggering actions via the Inspector (no coding needed).

Example:

using UnityEngine.Events;

public UnityEvent onDamageTaken;

Now in the Inspector, you can assign functions from other scripts to respond when this is invoked:

onDamageTaken.Invoke();

✅ UnityEvents are great for designers who don’t want to touch code.

Example: Trigger UI Update on Player Damage

Imagine a player takes damage and you want to update a health bar and play a sound.

Player Script

public class Player : MonoBehaviour
{
    public delegate void PlayerDamaged(int currentHealth);
    public static event PlayerDamaged OnPlayerDamaged;

    int health = 100;

    public void TakeDamage(int amount)
    {
        health -= amount;
        OnPlayerDamaged?.Invoke(health);
    }
}

UI Script

public class HealthBar : MonoBehaviour
{
    void OnEnable()
    {
        Player.OnPlayerDamaged += UpdateHealthBar;
    }

    void OnDisable()
    {
        Player.OnPlayerDamaged -= UpdateHealthBar;
    }

    void UpdateHealthBar(int health)
    {
        Debug.Log("Update UI: " + health);
    }
}

✅ No direct reference needed between Player and HealthBar. This is clean, modular, and reusable.

When to Use Delegates vs UnityEvents

Feature C# Delegates/Event UnityEvent (Inspector-based)
Best for… Developers & coders Designers & scripters
Performance Slightly faster Slightly slower
Inspector visible? ✅ Yes
Multicasting ✅ Easily support multiple ✅ Also supports multiple
Flexibility High Medium

Use Delegates when working in code-heavy systems.
Use UnityEvents when designers need control from Inspector.

✅ Real Use Cases

  • UI Systems → Update health, score, ammo
  • Audio Systems → Play SFX when buttons are clicked
  • Game Managers → Trigger next level, game over
  • Input → Respond to custom input actions

⚠️ Common Mistakes to Avoid

  • ❌ Forgetting to unsubscribe (-=) from events on OnDisable()
  • ❌ Invoking UnityEvent without null check (can cause runtime errors)
  • ❌ Overusing events for things better handled with interfaces or direct calls
  • ❌ Creating tightly coupled event chains (becomes hard to debug)

SEO Keywords Covered

  • Unity delegate tutorial
  • Unity event system explained
  • UnityEvents vs delegates
  • Unity custom event handler
  • Unity subscribe to event
  • Unity UI event trigger

Final Thoughts

Unity Events and Delegates are essential for building clean, scalable game architecture. Once you stop relying on drag-and-drop references and start using events for communication, your code becomes easier to debug, reuse, and extend.

Use UnityEvents for visual workflows
Use Delegates for modular code-driven systems

Are your scripts tightly coupled — or communicating cleanly like professionals?

Leave a Reply

Shopping cart

0
image/svg+xml

No products in the cart.

Continue Shopping