- Home /
How to fix the exception : ZenjectException: Unable to resolve type 'A' while building object with type 'B'. Object graph: B
I'm trying to load a prefab during runtime in one particular moment at my state machine. But I get this exception in runtime. All the scenes are validate with the Ctrl Left+Maj Left+V shortcut.
The context of the problem
My state machine is bind into the project context :
using Zenject;
namespace AVF.StateMachine
{
/**
* @class AVFStateMachineInstaller
* Class used to represent the installer of the state machine
*/
public class AVFStateMachineInstaller : MonoInstaller<AVFStateMachineInstaller>
{
/**
* Install the bindings between the objects
*/
public override void InstallBindings()
{
// Global factory
Container.Bind<AVFStateFactory>().AsSingle();
// States factories
Container.BindFactory<AVFInitState, AVFInitState.Factory>().WhenInjectedInto<AVFStateFactory>();
Container.BindFactory<AVFLoginState, AVFLoginState.Factory>().WhenInjectedInto<AVFStateFactory>();
Container.BindFactory<AVFSelectJobState, AVFSelectJobState.Factory>().WhenInjectedInto<AVFStateFactory>();
}
}
}
The state are create with a function CreateState in the AVFFactory class. No problem of creating states and navigate between them when I want. Except when I try to load a prefab who contains a game object context. So, I understand that all the managing of the state machine exist into the project context only.
I put, in the installer of the scene context, the bind of the factories of my prefabs :
using UnityEngine;
using Zenject;
using AVF.StateMachine;
public class AVFSceneHubInstaller : MonoInstaller<AVFSceneHubInstaller>
{
[SerializeField]
GameObject mPrefabWeldingPedestal;
[SerializeField]
GameObject mPrefabSandboxPedestal;
public override void InstallBindings()
{
Container.BindFactory<AVFSandboxPedestal, AVFSandboxPedestal.Factory>().FromSubContainerResolve().ByNewPrefab(mPrefabSandboxPedestal).WhenInjectedInto<AVFSelectJobState>();
Container.BindFactory<AVFWeldingPedestal, AVFWeldingPedestal.Factory>().FromSubContainerResolve().ByNewPrefab(mPrefabWeldingPedestal).WhenInjectedInto<AVFSelectJobState>();
}
}
My prefab contains a game object context with a installer :
using UnityEngine;
using Zenject;
public class AVFSandboxPedestalInstaller : MonoInstaller
{
[SerializeField]
private GameObject mPrefab;
[SerializeField]
private GameObject mAttachObject;
public override void InstallBindings()
{
Container.BindInterfacesAndSelfTo<AVFSandboxPedestal>()
.FromComponentInNewPrefab(mPrefab)
.UnderTransform(mAttachObject.transform)
.AsSingle();
}
}
And a component for using the object with the factory :
using UnityEngine;
using VRTK;
using Zenject;
public class AVFSandboxPedestal : VRTK_InteractableObject
{
public void Start()
{
Debug.LogWarning("HELLO SANDBOX");
}
public override void OnInteractableObjectUsed(InteractableObjectEventArgs e)
{
base.OnInteractableObjectUsed(e);
Debug.LogWarning(gameObject.name + "USED");
}
public class Factory : Factory<AVFSandboxPedestal>
{
}
}
My state who need the instance of the factory is like that and it's when I want to create this object I reach the exception because it can't resolve the type AVFSandboxPedestal.Factory :
using UnityEngine;
using Zenject;
using Mimbus.StateMachine;
using AVF.Context;
namespace AVF.StateMachine
{
/**
* @class AVFSelectJobState
* Class used to represent the select job state of the application
*/
public class AVFSelectJobState : MbStateEntity
{
// Class fields
private readonly AVFStateManager mStateManager; /**< The state manager */
private readonly AVFContext mContext; /**< Context of the application */
private readonly AVFHeadState mHeadState; /**< Common state of all states and scenes */
private readonly AVFSandboxPedestal.Factory mSandboxPedestalFactory; /**< */
/**
* Constructor
* @param[in] pStateManager The state manager of the application
* @param[in] pContext The context of the application
*/
public AVFSelectJobState(AVFStateManager pStateManager, AVFContext pContext, AVFHeadState pHeadState,
AVFSandboxPedestal.Factory pSandboxPedestalFactory)
{
this.mStateManager = pStateManager;
this.mContext = pContext;
this.mHeadState = pHeadState;
this.mSandboxPedestalFactory = pSandboxPedestalFactory;
}
/**
* Initialize this object
*/
public override void Initialize()
{
Debug.Log("Select job state start");
this.mSandboxPedestalFactory.Create();
}
// Other lines of code .....
What I already try
I think the problem is : I have my state machine and the state into the project context and my prefab in his own context. So, apparently, it can't work. (No?)
So I try to add a Zenject Binding component on my prefab, I add the AVFSandboxPedestal in it and add the project context in it too. But it doesn't work.
I try to add the installers too, to bind to the scene context, but nothing work. I'm out of solutions for that problem and reading the documentation over and over again don't help me right now. So I turn myself to you and ask for help.
NB : I don't want the actual prefab persist in all scenes just in the actual scene. But the state machine have to persist in all scenes.
NB : If you need more precisions. Just ask. ;)
Thanks ! :)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
GameObject.Find Unity 5.3+ 1 Answer
How to create a Drop Down menu for a script file? 1 Answer
Mobile swipes causing player to move different distances randomly 0 Answers