# Automatic Injection

In this scene, we demonstrate an example of automatic injection. Instead of manually passing the runtime variables to other components, we will use the Runtime Injector. Select **enemy\_runtime\_autoInjection** in the scene.

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

You can see that this component will create and inject two variables: **hp** and **speed**.\
For the **hp** variable, it will create a deep copy of the example variable **example\_float\_enemyHealthTemplate**. This is useful because we can predefine a max value.

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

Next, it will inject the **hp** variable into both the **RuntimeHealthInjected** component and the **HealthBarSpriteAutoInjection** component.

<figure><img src="/files/2DBbTKyYB9ww3HzhgG9k" alt=""><figcaption></figcaption></figure>

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

For the **speed** variable, it will create a brand-new variable (without a template) and inject it into **EnemyMovementInjected.cs**.

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

If we look at the code, the fields to be injected are simply marked with the `[RuntimeInjectable]` attribute. Here is what **RuntimeHealthInjected.cs** looks like:

```csharp
public class RuntimeInjectedHealth : MonoBehaviour
{
    [Tooltip("This field will be injected at runtime")] 
    [SerializeField]
    [RuntimeInjectable("hp")] 
    private FloatVariable _runtimeHpVariable;

    [SerializeField] private FloatReference _maxHealth;
    
    private void Start()
    {
        _runtimeHpVariable.Value = _maxHealth.Value;
        _runtimeHpVariable.OnValueChanged += OnHealthChanged;
    }

    private void OnDisable()
    {
        _runtimeHpVariable.OnValueChanged -= OnHealthChanged;
    }

    private void OnHealthChanged(float newValue)
    {
        if (newValue <= 0f)
            gameObject.SetActive(false);
    }

    //In this example, this is called when the enemy collides with the Player.
    public void TakeDamage(int amount) => _runtimeHpVariable.Add(-amount);
}
```

As you can see, this approach eliminates much of the manual work previously required, such as creating and injecting the variables.

**Note**: The `RuntimeInjector` calls its `Awake` method before the default components. This means that other components can directly access the injected variables in their `Awake` methods (as shown in **RuntimeHealthInjected.cs**).


---

# 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://obvious-game.gitbook.io/soap/scene-documentation/8_runtimevariables/automatic-injection.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.
