Search code examples
unity-game-enginezenject

Not resolved dependency in instantiated gameobject


Installer:

class MainInstaller : MonoInstaller<MainInstaller>
{
    [SerializeField]
    private StateController StateController;

    public override void InstallBindings()
    {
        Container.BindInstance( StateController );
    }
}

Behaviour with injection:

class Foo : MonoBehaviour
{
    [Inject]
    private StateController StateController;

    void Start()
    {
        UpdateObject( StateController.Current );
    }
}

Problem is StateController is null at UpdateObject.

Should I instantiate gameobject somehow especially?


Solution

  • Here is the answer https://github.com/modesttree/Zenject/issues/57.

    GameObject.Instantiate does not resolve dependencies. One should use DiContainer.InstantiatePrefab or use Factories.