# 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**

![](https://963537199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lzhr1XSMzMqNXjRnNlb%2F-M-zhmrZTEHCtQaCa7R1%2F-M-zkvcDk1-Y6f9_010V%2Fimage.png?alt=media\&token=4af04b4b-fe09-46f1-9d0c-a9922c98df2e)

### 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.

![](https://963537199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lzhr1XSMzMqNXjRnNlb%2F-M-zhmrZTEHCtQaCa7R1%2F-M-zl_7Wv9bZsBhdOPiB%2Fimage.png?alt=media\&token=16686a47-129b-428d-a52a-75f9d5624aab)

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

![](https://963537199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lzhr1XSMzMqNXjRnNlb%2F-M-zhmrZTEHCtQaCa7R1%2F-M-zm5FNiEAiGEzt2B7f%2Fimage.png?alt=media\&token=3ac435bc-33d8-4aae-8e09-06bebd5469b1)

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

![](https://963537199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lzhr1XSMzMqNXjRnNlb%2F-M-zhmrZTEHCtQaCa7R1%2F-M-zmbY1kVef6MGfL4Pu%2Fimage.png?alt=media\&token=c04ffe73-298a-4ef5-943a-416998b2492f)

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