> 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/8_runtimevariables/automatic-injection.md).

# 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**).
