Unit Destroyed

public event UnitAction UnitDestroyed

Note

This used to be a static event on the Unit class itself. It has since moved to UnitsService as a regular instance event — inject UnitsService to subscribe to it, as shown below.

This event is fired every time any unit is destroyed.

Example

For example, you can do something when any unit is destroyed. You can receive info about the death by connecting to the UnitsService.UnitDestroyed event.

// this example code can be run from any MonoBehaviour script

[Inject] UnitsService units;

void Start()
{
  units.UnitDestroyed += OnUnitDestroyed; // now every time any unit is destroyed, the OnUnitDestroyed method will be called and...
}

void OnUnitDestroyed(Unit unit)
{
  Debug.Log("Unit was destroyed: " + unit.name); // ... this message will appear in the Console.
}

void OnDestroy()
{
  units.UnitDestroyed -= OnUnitDestroyed; // don't forget to unsubscribe!
}