> For the complete documentation index, see [llms.txt](https://malbersanimations.gitbook.io/animal-controller/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://malbersanimations.gitbook.io/animal-controller/how-to-ac/listen-to-modes.md).

# Listen when a Mode Starts or Ends

You can use Unity Events for the mode to let you know when a mode starts or ends.

## Subscribe to Modes via Inspector

### How to Listen to **any** *Mode.*

Use the global Unity Events **On Mode Start** and **On Mode End**

![](/files/-M-zkvcDk1-Y6f9_010V)

### How to Listen to a **specific** *Mode* (e.g. Attack1)

On the Global Properties section of every **Mode**, you can subscribe to the Unity Events **On Enter | On Exit** to listen when the Main Attack (`Attack1`) starts or ends.

![](/files/-M-zl_7Wv9bZsBhdOPiB)

### How to listen to an *Ability* inside a *Mode* (E.g. Subscribe to ***Eat*** Action)

If you want to listen to a specific Ability, for example **Eat**, then you can use the Override Properties:

![](/files/-M-zm5FNiEAiGEzt2B7f)

{% hint style="danger" %}
Set the Override **Affect** States Option Select **NONE** (that way the Ability will use the Global Affect parameters)
{% endhint %}

![](/files/-M-zmbY1kVef6MGfL4Pu)

## Subscribe to Modes via Script

### How to Listen to **any** *Mode.*

```csharp
void Awake()
{ 
   animal = GetComponent<MAnimal>(); 
}

void OnEnable()
{ 
    animal.OnModeStart.AddListener(StartMode); 
    animal.OnModeEnd.AddListener(EndMode); 
}

void OnDisable()
{
    animal.OnModeStart.RemoveListener(StartMode);
    animal.OnModeEnd.RemoveListener(EndMode);
}


void StartMode(int ModeID)
{
    //Your code Here
}

void EndMode(int ModeID)
{
    //Your code Here
}
```

### How to Listen to a **specific** *Mode* (e.g. ***Action***)

```csharp
private MAnimal animal;
private Mode ActionMode;

void Awake()
{ 
    animal = GetComponent<MAnimal>(); 
    
    ActionMode = animal.Mode_Get(4);  //4 is the ID for Action
}

void OnEnable()
{   
    if (ActionMode != null)
    {
        ActionMode.GlobalProperties.OnEnter.AddListener(StartAction);
        ActionMode.GlobalProperties.OnExit.AddListener(EndAction);
    }
}

void OnDisable()
{
    if (ActionMode != null)
    {
        ActionMode.GlobalProperties.OnEnter.RemoveListener(StartAction);
        ActionMode.GlobalProperties.OnExit.RemoveListener(EndAction);
    }
}

void StartAction()
{
    //Your code Here
}

void EndAction()
{
    //Your code Here
}
```

### How to listen to an *Ability* inside a *Mode* (E.g. Subscribe to ***Eat*** Action)

```csharp
 private MAnimal animal;
 private Ability EatAbility;


void Start()
{
    animal = GetComponent<MAnimal>();          
    Mode ActionMode = animal.Mode_Get(4);         //4 is the ID for Action

    if (ActionMode != null)
    {
        EatAbility = ActionMode.Abilities.Find(ability => ability.Name == "Eat");
    }
}

void OnEnable()
{
    if (EatAbility != null)
    {
        EatAbility.OverrideProperties.OnEnter.AddListener(StartEat);
        EatAbility.OverrideProperties.OnExit.AddListener(EndEat);
    }
}

void OnDisable()
{
    if (EatAbility != null)
    {
        EatAbility.OverrideProperties.OnEnter.RemoveListener(StartEat);
        EatAbility.OverrideProperties.OnExit.RemoveListener(EndEat);
    }
}

void StartEat()
{
    //Your code Here
}

void EndEat()
{
    //Your code Here
}
```
