> ## Documentation Index
> Fetch the complete documentation index at: https://docs-unity.molca.id/llms.txt
> Use this file to discover all available pages before exploring further.

# Dependency injection

> Inject attribute, scene injection, and manual resolution for dynamically created objects.

**Attribute:** `[Inject]` — `Packages/com.molca.core/Runtime/Attributes/InjectAttribute.cs`\
**Resolver:** `RuntimeManager`

## Behaviour fields

`MonoBehaviour` fields and properties marked `[Inject]` are filled after runtime initialization when the manager scans the scene.

## Dynamic objects

* `RuntimeManager.InjectDependencies(instance)`
* `RuntimeManager.CreateWithInjection<T>()` for types with constructor injection

## Constructor injection

Supported for non-Unity types created through the injection factory (see [RuntimeManager](/core/runtime-manager) source).

## Example: consumer `MonoBehaviour`

Scene objects are discovered after **`RuntimeManager`** finishes startup; fields stay **`null` in `Awake`** until injection runs. For anything in **`Start`** or later, **`await RuntimeManager.WaitForInitialization()`** first (see also [Sequence controller](/core/sequence-controller) for the same pattern on `EventDispatcher` and `ReferenceManager`).

```csharp theme={null}
using UnityEngine;
using Molca;
using Molca.Events;
using Molca.ReferenceSystem;

public class MyFeatureController : MonoBehaviour
{
    [SerializeField] private string displayName = "Feature";

    // Filled automatically once RuntimeManager has registered services and scanned the scene.
    [Inject] private EventDispatcher _eventDispatcher;
    [Inject] private ReferenceManager _referenceManager;

    // Optional: allow missing service without logging an error (default Required = true).
    // [Inject(Required = false)] private IOptionalSubsystem _optional;

    private async void Start()
    {
        await RuntimeManager.WaitForInitialization();

        if (_eventDispatcher == null || _referenceManager == null)
        {
            Debug.LogError($"{nameof(MyFeatureController)}: injection failed; check RuntimeManager and registrations.");
            return;
        }

        // Use injected services from here on.
    }
}
```

### Objects created at runtime

`AddComponent` / `Instantiate` does not get the automatic scene pass. Call **`RuntimeManager.InjectInto(component)`** (or **`InjectDependencies`** on the instance) after the object exists and **`RuntimeManager.IsReady`** is true.

```csharp theme={null}
var go = new GameObject("RuntimeWorker");
var worker = go.AddComponent<MyFeatureController>();
RuntimeManager.InjectInto(worker);
```

## Related

* [Recipe: Implement dependency injection](/recipes/implement-dependency-injection) — step-by-step guide to using \[Inject] attribute in custom components
