0

Zenject doesn't work with Behavior Disiner. When injected into Action,injection does not occur.

[Inject]
public void Constructor(GameStateHandler gameStateHandler)
{
    gameStateHandler.OnGameStart(() => _isGameStart = true);
    Debug.Log(2);
}

Debug does not receive logs of the fact that it enters the constructor method, if I use fields, then it is also null.

I expecting injected into Action. Zenject doesn't work with Behavior disiner?

Danil
  • 1

1 Answers1

0

You need to show also the binding stage of your code. You need to have this in place and check that the bindings run. From the documentation:

"In Zenject, dependency mapping is done by adding bindings to something called a container. The container should then 'know' how to create all the object instances in your application, by recursively resolving all dependencies for a given object."

So for your class, imagine named Foo:

public class Foo
{
    IBar _bar;

    public Foo()
    {
    }

    [Inject]
    public void Constructor(GameStateHandler gameStateHandler)
    {
        gameStateHandler.OnGameStart(() => _isGameStart = true);
        Debug.Log(2);
    }
}

You need to have your bindings previously:

public class FooInstaller : MonoInstaller
{
    public override void InstallBindings()
    {
        Container.Bind<Foo>().AsSingle();
        Container.Bind<GameStateHandler>().AsSingle();
    }
}

Everything is explained in the docs. Once you have your FooInstaller in place, the InstallBindings methods will run when you start your app. That will make the container know what to supply when an instance of Foo is requested in the code.

rustyBucketBay
  • 4,320
  • 3
  • 17
  • 47