Defining a component from a list when an event is raised

Good afternoon. I have a list of components, in OnEnable I subscribe to the event of these components. How to determine which component in the list triggered an event?

public class Example : MonoBehaviour
{
    [SerializeField] private List<Components> _components;

    private Component _currentComponent;

    private void OnEnable()
    {
        foreach (var component in _components)
        {
            component.Event+= Method;
        }
    }

    private void OnDisable()
    {
        foreach (var component in _components)
        {
            component.Event-= Method;
        }
    }

    private void Method()
    {
        _currentComponent = ???????
    }
}

The only solution I found is when calling an event in the “Component” script via transform.GetComponent to pass data, but it seems to be costly

Define your event with a parameter, such as Action<Component>, and just have the component pass themselves through when they raise the event.

1 Like