Applies to: Molca CoreDocumentation 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.
Overview
This recipe shows you how to use the[Inject] attribute to access RuntimeManager services (like EventDispatcher, DataManager, ReferenceManager) in your custom MonoBehaviour components. Dependency injection eliminates the need for singleton patterns or manual service lookups, making your code cleaner and more testable. You’ll learn how to inject services into scene objects and runtime-created objects.
Prerequisites
- SDK modules: Molca Core installed
- Unity setup: RuntimeManager configured in your scene
- Prior knowledge: RuntimeManager, Dependency injection
Step-by-step
Step 1: Add [Inject] attribute to service fields
Mark service fields with the[Inject] attribute. The RuntimeManager will automatically populate these fields after initialization.
RuntimeManager scans all scene MonoBehaviour components after initialization and fills fields marked with [Inject]. The Required parameter controls whether a missing service logs an error (default: true).
Step 2: Wait for RuntimeManager initialization
Always callawait RuntimeManager.WaitForInitialization() in Start before using injected services. Injected fields are null in Awake and early Start.
RuntimeManager.WaitForInitialization() blocks until RuntimeManager.IsReady is true, which happens after all subsystems are initialized and scene injection is complete.
Step 3: Inject dependencies into runtime-created objects
Objects created withInstantiate or AddComponent don’t get automatic injection. Call RuntimeManager.InjectDependencies() manually after creating them.
RuntimeManager initialization. Runtime-created objects must call InjectDependencies() explicitly to populate their [Inject] fields.
Step 4: Use InjectDependencies pattern for late initialization
If your component might be created beforeRuntimeManager is ready, use the InjectDependencies pattern to handle both cases.
Complete example
Here’s a complete example showing dependency injection in a custom training step that uses multiple services:- Injecting multiple services (
EventDispatcher,ReferenceManager) - Waiting for initialization in
Start - Using injected services in lifecycle methods
- Proper cleanup in
OnStepDeactivated
Troubleshooting
- Injected fields are null: Ensure you call
await RuntimeManager.WaitForInitialization()before accessing injected services. Fields arenulluntil the injection pass completes. - Runtime-created objects have null fields: Objects created with
InstantiateorAddComponentdon’t get automatic injection. CallRuntimeManager.InjectDependencies(instance)after creating them. - “Service not registered” error: The service you’re trying to inject isn’t registered with
RuntimeManager. Check that the correspondingRuntimeSubsystemis attached to the RuntimeManager prefab and enabled. - Injection works in Editor but not in build: Verify that the RuntimeManager prefab is referenced in Molca Project Settings and that the prefab is included in the build.
- Multiple injection calls cause issues: Calling
InjectDependencies()multiple times on the same object is safe — it will re-inject fields but won’t cause errors.
Related
- Dependency injection — detailed documentation on the injection system
- RuntimeManager — service container and initialization manager
- EventDispatcher — commonly injected service for event communication
- ReferenceManager — commonly injected service for object lookup