1

I'm getting a ZenjectException: Unable to resolve 'Facade'. Object graph: error that I can't understand.

I've been following this example but I can't get my code to work.

I have a prefab with the following components:

  • Facade
  • FacadeInstaller
  • Game Object Context (that references the FacadeInstaller above)

The Facade class:

using UnityEngine;
using Zenject;

public class Facade : MonoBehaviour
{
    public class Factory : PlaceholderFactory<Argument, Facade> { }
}

The FacadeInstaller class:

public class FacadeInstaller : MonoInstaller
{
    [Inject]
    Argument arg;

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

The Argument class:

public class Argument
{
    public string val;
}

In the scene I have a SceneContext that references the following installer:

using UnityEngine;
using Zenject;

public class SceneInstaller : MonoInstaller
{
    public GameObject prefab;

    public override void InstallBindings()
    {
        Container.BindFactory<Argument, Facade, Facade.Factory>()
            .FromSubContainerResolve()
            .ByNewContextPrefab<FacadeInstaller>(prefab);
    }
}

where prefab is linked in the inspector to the one I detailed above.

If I leave at that, the scene validates successfully.

Now the problem presents itself when I add another game object to the scene with the following component:

using UnityEngine;
using Zenject;

public class Creator : MonoBehaviour
{
    [Inject]
    Facade.Factory factory;
}

and the scene validations fails with the error:

ZenjectException: Unable to resolve 'Facade'. Object graph: 

What's wrong with my setup?

Daniele
  • 153
  • 3
  • 11
  • I've never used Zenject before, but nothing appears to be bound to Creator, so it doesn't know what to inject? Something that performs the role of `ShipInstaller` from the documentation you linked. – NSJacob1 Dec 15 '20 at 16:19

1 Answers1

0

Шt is clear that 2 years have already passed, but still. You just forgot to add the class to monoinstallers. Maybe this will help someone.

  • I'm not following that example, but I'm having the same error trying to bind a factory with an installer and a (nested) sub container. Which class are you talking about, exactly? The factory is added and resolved without any problem, I have a problem only when the object generated by the facade is resolved, and that's true even if I remove all the parameters and the injected values from the generated class and factory. And maybe I'm missing something, but I'm not able to spot any particular binding of the generated class into the example. – Totoro May 28 '23 at 17:59
  • Ok, I figured out the problem. Essentially, when something in a factory is registered with a sub container and an installer, the installer (or another installer in the same context) is responsible to expose the instance created by the factory to the container. That was not clear to me at first, and it took me a while to figure it out. In my case I just done a Container.Bind().AsSingle() inside the installer and all worked. – Totoro May 28 '23 at 21:08