Event is a special kind of delegate:
Step 1: declare an delegate:
public delegate void ClickAction();
Step 2: Setup the event
public static event ClickAction OnClicked;
Step 3: Create the function
void Teleport() { Vector3 pos = transform.position; pos.y = Random.Range(1.0f, 3.0f); transform.position = pos; }
Step 4: Setup Listener
Eventmanager.OnClicked += Teleport;
Step 5: Invoke the event
void OnGUI() { if(GUI.Button(new Rect(Screen.width / 2 - 50, 5, 100, 30), "Click")) { if(OnClicked != null) OnClicked(); } }
Step 5: Remove Listener
Eventmanager.OnClicked -= Teleport;