Unity Action events

Richard Morgan
Aug 10, 2022

Here’s a simple example that shows how Action can replace delegate/event statements and accomplish the exact same thing.

Here we have a UI script that needs to be updated whenever the Damage method of the Player script is executed.

We declare our Action in the Player and set it to static so the other scripts can access it directly. In our UI script, we add the UpdateHealth method to the new Action. Then, back on the Player script, we trigger the Action when the Damage method is executed.

This separates our scripts so that one script does not have to know the other exists. The scripts can operate independently and now the scripts are reusable.

--

--