# Listen when an Mode starts or ends

You can use the Unity Events for the mode to let you know when a mode start 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 an  specific Ability (E.g. 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.OnModeStart.AddListener(StartMode); 
}

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


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: 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:

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

The question should be specific, self-contained, and written in natural language.
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.
