# Selectable Unit

## Overview

<mark style="color:blue;">**SelectableUnit**</mark> component implements state management for the unit selection along with updating the **Selection Indicator** reference (if one is set). This component provides out of the box functionality for unit selection by implementing <mark style="color:blue;">**ISelectable**</mark> interface. This is default single selection behaviour, for selecting a unit as group of units see the extension component [selectable-group-unit](https://travljen.gitbook.io/unit-selection/how-it-works/selectable-group-unit "mention").

## Configuration

There are only few things this component does, so options to customise also reflect this.

* **Selection Indicator** reference is used for updating visual state of the unit.

  *More details selection indicators can be found here* [selection-indicator](https://travljen.gitbook.io/unit-selection/how-it-works/selectable-unit/selection-indicator "mention")*.*
* **Indicator Priority** defines which of the visual states is shown when both are active (selected and highlighted). Simple example of this scenario would be when unit is already selected and mouse hovers over it. Should it keep selected state showing or override it with highlighted state.
* **OnSelectionStateChange** is Unity Event invoked when any of the selection states on unit change. You can use this to update custom any data needed managed by your components or updating custom selection indicator that does not use <mark style="color:blue;">**ASelectionIndicator**</mark> abstraction

<figure><img src="https://425343605-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fml7vS0D5xN2Q8jQMEmBv%2Fuploads%2FAnjYfwcmX1xOHmuCcwLT%2Fimage.png?alt=media&#x26;token=0c90f12e-fcd4-44cc-92b6-b3b60ea139c2" alt="" width="563"><figcaption><p>SelectableUnit component</p></figcaption></figure>

## Customisation

Communication between units and selection system is done with <mark style="color:blue;">**ISelectable**</mark> interface. To replace the use of <mark style="color:blue;">**SelectableUnit**</mark> component, create or use existing mono behaviour to implement your own management of the unit state. Example:

```csharp
class MySelectionUnit : MonoBehaviour, ISelectable // ISelectable is key here
{
    // And implementation of required properties and methods for the interface.
    public bool IsSelected { get; private set; } = false;

    public bool IsHighlighted { get; private set; } = false;

    public void Select()
    {
        // Select my unit
        IsSelected = true;

        // Here you can update your visuals for the unit
    }

    public void Deselect()
    {
        // Deselect my unit
        IsSelected = false;
    }

    public void Highlight()
    {
        // Highlight my unit
        IsHighlighted = true;
    }

    public void Unhighlight()
    {
        // Unhighlight my unit
        IsHighlighted = false;
    }
}
```


---

# 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://travljen.gitbook.io/unit-selection/how-it-works/selectable-unit.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.
