Passing arguments with EventHandlers

Is there a way to pass custom arguments with EventHandlers?

IE:

public event EventHandler OnDamagedEvent;

public void SomeTakeDamageMethodEtc()
{
    ////Calculate damage etc etc
    OnDamagedEvent?.Invoke(this, EventArgs.Empty, damageTaken);
}

I’ve been struggling trying to figure this out the simplest way possible.

You’ll need to create a Unity Event class…

Examples
UnityEvent
UnityEvent<T0,T1>
UnityEvent<T0,T1,T2>
UnityEvent<T0,T1,T2,T3>

Hope this helps.

To expand on what @Vectorbox said, if you create a class from a unity event, you can also populate a unity event in the inspector.

    using UnityEngine.Events;

    //....

    [Serializable]
    public class IntEvent : UnityEvent<int> { }

    public IntEvent OnDamagedEvent;

    public int DamageTaken;

    public void SomeTakeDamageMethod()
    {
        OnDamagedEvent.Invoke(DamageTaken);
    }
2 Likes