[Unity] Event Systems

//public event EventHandler Something;    Event has sender and receiver relationship
//Something(this, EventArgs.Empty);    First parameter is the sender
//public event Action Something = () => {};

//****Script 1
using System;

public static event Action<int> Something;    //Action is an Event with return void and no args, it sends arg to all subscribers
//event keyword prevents the delegate to be directly assigned or called

OnCollisionEnter2D(Collision2D collision) {
Something?.Invoke(1);
}

//****Script 2
Script1.Something += FunctionOne;   //no need to import Script1, no need of instance b/c static
Script1.Something += FunctionTwo;

private void OnDestroy() {
Script1.Something -= FunctionOne;
Script1.Something -= FunctionTwo;
}

private void FunctionOne(int num) {

}

Scroll to Top