Search code examples
unity-game-enginezenject

Inject components attached to child GameObjects of prefab


I use Factories to create GameObjects from prefabs.

MenuScreen (prefab root game object)
  * MenuScreenComponent <- inject ScreensNavigator
  - Button (game object)
    * ButtonSoundBehaviour <- inject AssetsProvider

In the installer:

Container.BindFactory<MenuScreen, MenuScreen.Factory>().FromComponentInNewPrefab(MenuFragmentPrefab);

ScreensNavigator injected into MenuScreenComponent without any problems.

How to make ButtonSoundBehaviour to be injected with AssetsProvider? I tried to add Zenject Binding to Button GameObject but it doesn't help.


Solution

  • You need to tell zenject where to find AssetsProvider by adding a binding for it on the container.

    For example, if you add Container.Bind<AssetsProvider>().AsSingle() to an installer, then ButtonSoundBehaviour will have AssetsProvider injected into it.