# Reactions

## Overview

Reactions are snippets of code you can add to your scripts to execute more precise logic on the animal or on any components. Instead of using Unity Messages or Unity Events to connect them.

## How to use them

### Reactions Component

You can add the Reactions Component to any GameObject to access them

<figure><img src="/files/QsaHGtISvettkuDPJyLJ" alt=""><figcaption></figcaption></figure>

Once you have added you can use the `React(Component Target)` method to activate it. Whether is via a Unity Event or by code. You need to give the Reaction a Target to apply the reaction.

<figure><img src="/files/3JFn8dCbHEnKT2Tydh7S" alt=""><figcaption></figcaption></figure>

### &#x20;Reaction Scriptable Var

To create a new Reaction go to the Menu:

&#x20;Create > Malbers Animations > Reaction Var

<figure><img src="/files/acHRYWwmdS09EsfIXUfI" alt=""><figcaption></figcaption></figure>

To make use of the Reaction Var you can add them to any Unity Event that has a GameObject, Monobehaviour, Transform, or Animal as a parameter.

![Using a Speed Reaction to Activate the Walk Speed Modifier to the Animal](/files/XhDgr63XBN2OHjl3wRYj)

E.g. You can Disable the Jump State when entering a room ("Box Collider"). and enable it back when exiting:

{% embed url="<https://streamable.com/xnib62>" %}

### Scripting

You can also add a reaction to your code and use it just like any other event.

This is the correct syntax:

<pre class="language-csharp" data-full-width="false"><code class="lang-csharp"><strong>[SerializeReference,SubclassSelector]
</strong>public Reaction reaction;
</code></pre>

You need to add  `[SerializeReference,SubclassSelector]` to display it right on the inspector.

To call a reaction simply use this code:

```csharp
reaction?.React(component); //Add a Target to the reaction, [Component]
//OR
reaction?.React(gameObject); //Add a Target to the reaction, [GameObject]
```

<figure><img src="/files/pzE2DnZukshbO7yAHDcs" alt=""><figcaption><p>Example of MDamageable Component using reactions.</p></figcaption></figure>

## Creating a new Reaction Type

You can create your own reactions. Here's a quick example of how to create a reaction that enables or disables a collider:

{% embed url="<https://streamable.com/2wpcai>" %}

{% code overflow="wrap" %}

```csharp
using MalbersAnimations;
using MalbersAnimations.Reactions;
using System; 
using UnityEngine;

[System.Serializable] //Needs to be Serializable!!!!
[AddTypeMenu("My Reaction/Enable Collider")]
public class EnableColliderReaction : Reaction
{
    //set the Type of component this Reaction Needs
    public override Type ReactionType => typeof(Collider); 
    
    public bool enable;

    protected override bool _TryReact(Component reactor)
    {
        Collider collider = reactor as Collider; //Cast the reactor as collider type.
        collider.enabled = enable;              //set the enable paramater

        return true; //Reaction succesful!!
    }
}

```

{% endcode %}


---

# 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/main-components/reactions.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.
