> For the complete documentation index, see [llms.txt](https://obvious-game.gitbook.io/soap/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://obvious-game.gitbook.io/soap/scene-documentation/9_scriptabledictionaries/adding-and-removing-elements.md).

# Adding & Removing Elements

To add an entry into a scriptable dictionary is the same as adding an entry in a regular dictionary.&#x20;

In this example, each element prefab has a component "AddRemoveElementToDictionary" attached to it. Navigate to the hierarchy and select one element template object (you can choose any element, but I will show Water):

<figure><img src="/files/ACs1HIOxUFDuk7nDqEwF" alt="" width="222"><figcaption></figcaption></figure>

Now, look in the inspector to check out its components:&#x20;

<figure><img src="/files/dYmnN3HTkhNC5e29Dn0J" alt="" width="375"><figcaption></figcaption></figure>

The nice part about using a Dictionary, is that we can have different cases when the item is already in the dictionary or not. Here is how an entry is added or removed:

```csharp
private void Start()
{
    _element = GetComponent<Element>();

    //Try to add the first element of this type
    if (!_scriptableDictionary.TryAdd(_element.ElementType, 1))
    {
        //If its already in, just increment the count
        _scriptableDictionary[_element.ElementType]++;
    }
}

private void OnDestroy()
{
    //Decrement the count of the element
    _scriptableDictionary[_element.ElementType]--;

    //If the count is 0, remove the element from the dictionary
    if (_scriptableDictionary[_element.ElementType] == 0)
    {
        _scriptableDictionary.Remove(_element.ElementType);
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://obvious-game.gitbook.io/soap/scene-documentation/9_scriptabledictionaries/adding-and-removing-elements.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.
