hadashiA / VContainer

The extra fast, minimum code size, GC-free DI (Dependency Injection) library running on Unity Game Engine.
https://vcontainer.hadashikick.jp
MIT License
1.89k stars 165 forks source link

[Inject] Using wrong resolver, multiple LifetimeScopes #577

Open brangaman opened 10 months ago

brangaman commented 10 months ago

I have an issue where I have a couple of nested prefabs with Monobehaviours using [Inject] in my game scene which is part of my game scene lifetime scopes auto inject gameobjects. In the previous scene there is a LifetimeScope which registers the majority of the services etc before transitioning to the game scene, this persists across scenes.

Once the game scene is loaded the prefabs that should be auto injected by my games scenes LifetimeScope are being injected by the previous loading scene LifetimeScope that has persisted.

Is there a way to prevent this?

AlonTalmi commented 10 months ago

From my understanding you registered services in a custom LifetimeScope inside the loading scene, and then loaded a new additive scene and all injections at that scene are from the previous scene?

I suspect something else is going on, can you share more info of your setup? LifetimeScope doesn't automatically inject to newly loaded scenes or instantiated object unless explicitly asked to.

If you want to pass injections from one scene to another you usually need Enqueue the parent scope (See here)