---
description: Learn how works! Managing selectable unit references for the selection system.
---

# Unit Management

## Introduction

This topic is generally the last one you'll need to consider, and many games might not require any changes at all. However, we provide public interfaces allowing **full customisation** of this behaviour should you need to manage it yourself. Provided component **ManageUnitObject** implements this behaviour for you, if a custom one is used remove this component from your game objects.&#x20;

## Overview

The unit manager is crucial for supplying valid selectable units to the selection system. This communication is facilitated by the `IUnitManager` interface. It's primary function is to provide a list of valid units for selection.

### IUnitManager Interface

```csharp
public interface IUnitManager
{
    public List<ISelectable> SelectableUnits { get; }
}
```

By implementing this interface you should provide custom list of valid units at any point, like switching between different lists based on some player action. Though for filtering itself there is a more straightforward approach using filter [#ifilterselection-interface](../active-selections#ifilterselection-interface "mention"). Note that if the unit management interface has no selectable units, **drag selection** will not work, only units present in the list may be selected.

## How it works

Using the **ManageUnitObject** component on selectable units means that the **UnitManager** handles everything, requiring no further action on your part.

### **UnitManager**

This singleton manages all active and valid selectable units that have the **ManageUnitObject** component. It provides interfaces to add, remove, and clear units, although typically, you won't need to use these directly.&#x20;

### UnitManagementEvents

Provides events for managing unit lifecycle events within the game. This class is essential for cleaning up no longer valid unit references in the selection systems. It currently holds only one event named **OnUnitRemoved** and it should be invoked when unit is no longer valid for selection (was removed from **SelectableUnits** list). This makes sure that any reference using this object is removed and prevents potential null reference exceptions.

If [#manageunitobject-component](#manageunitobject-component "mention") is used, this is handled internally. Read it's documentation for further information.

### ManageUnitObject Component

This component is part of drag selection feature and can be added through **SelectableUnit** component or manually. It notifies the **UnitManager** singleton about any changes in the unit's state like when they are instantiated, enabled, disabled, unloaded or otherwise destroyed.



## Customisation

If you desire to create your own behaviour instead of using this singleton, you replace it by implementing your own `IUnitManager`. All code is accessible and well documented, adjusting it or create your own.

In case you into problems creating your own manager, we are always here for you [#general-support](../../welcome/support-and-community#general-support "mention").



### Unit Destruct or Deactivation

When selectable units are no longer valid for selection, destroyed or otherwise disabled, selection system **must** be notified to prevent using no longer valid game objects. It would look something like this:

```csharp
// Do this on unit when deactivating or destroying your unit
ISelectable selectableUnit = GetComponent<ISelectable>();
UnitManagementEvents.OnUnitRemoved.Invoke(selectableUnit);
```

This is to prevent null references inside the system; for performance gain it never checks for a null reference.
