# Writing Custom State Behaviours

One of the most powerful components coming with Visual State Machine is the **StateBehaviour** class. It allows you to easily implement the [State Design Pattern](https://gameprogrammingpatterns.com/state.html#the-state-pattern) and create custom MonoBehaviours for your different states. This can for example be used to implement state behaviours for AI characters, game states or UI menus.

```
using Ilumisoft.VisualStateMachine;
using UnityEngine;

/// <summary>
/// Derive from StateBehaviour to execute custom logic when a state is executed.
/// </summary>
public class MenuStateBehaviourExample : StateBehaviour
{
    /// <summary>
    /// The id of the state the behviour should act on
    /// </summary>
    public override string StateID => "Menu";

    protected override void OnEnterState()
    {
        // Execute something when the state is entered
        Debug.Log($"On Enter State: '{StateID}'");
    }

    protected override void OnExitState()
    {
        // Execute something when the state is exited
        Debug.Log($"On Exit State: '{StateID}'");
    }

    protected override void OnUpdateState()
    {
        // Execute something when the state is updated...
    }
}

```

To use a custom State Behaviour script, simply add it to a Game Object and assign the State Machine it should be executed on.

<figure><img src="https://3979764177-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MCgKLjperRFhHlR4O0p%2Fuploads%2FoAmdVsEpGaRfICFejyAO%2FMenuStateExampleBehaviour.jpg?alt=media&#x26;token=9820a9e1-80c9-498d-a288-d7b9d6f3bddf" alt=""><figcaption></figcaption></figure>
