> 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
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://malbersanimations.gitbook.io/animal-controller/how-to-ac/listen-to-modes.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
