ssannandeji / Zenject-2019

Dependency Injection Framework for Unity3D
MIT License
2.52k stars 363 forks source link

FromNewComponentOn not working #569

Closed ManHunterGroms closed 6 years ago

ManHunterGroms commented 6 years ago

My code:

    public class ConcreteCharacterInstaller : MonoInstaller<ConcreteCharacterInstaller>
    {
        [SerializeField] private Animator _animator;
        [SerializeField] private MeshRenderer _meshRenderer;

        public override void InstallBindings()
        {
            Container.Bind<Transform>().FromComponentOn(gameObject).AsSingle();
            Container.Bind<NavMeshAgent>().FromComponentOn(gameObject).AsSingle();
            Container.Bind<Rigidbody>().FromComponentOn(gameObject).AsSingle();
            Container.Bind<Animator>().FromInstance(_animator).AsSingle();
            Container.Bind<MeshRenderer>().FromInstance(_meshRenderer).AsSingle();

            Container.Bind<CharacterDebug>().FromNewComponentOn(gameObject).AsSingle();
        }
}

Character creating from factory (FromComponentInNewPrefabResource):

            Container.BindFactory<CharacterFacade, CharacterFacade.Factory>()
                .FromComponentInNewPrefabResource($"{PrefabsPath}").AsSingle();

After creating CharacterDebug no added.

svermeulen commented 6 years ago

Is it a game object context? What is installing ConcreteCharacterInstaller?

ManHunterGroms commented 6 years ago

Yes, GameObjectContext. ConcreteCharacterInstaller - is character prefab installer.

svermeulen commented 6 years ago

So PrefabsPath has a GameObjectContext on it with another prefab with ConcreteCharacterInstaller on it dragged into the Prefab Installer property? ConcreteCharacterInstaller isn't attached to the same game object as GameObjectContext?

ManHunterGroms commented 6 years ago

My prefab setup: image

ManHunterGroms commented 6 years ago

I found mistake. I dont inject CharacterDebug in any scripts, but after added: [Inject] private CharacterDebug _characterDebug; into CharacterFacade it works!

But I want only add CharacterDebug to gameObject and Inject dependency (NavMeshAgent and Transform).

Sorry, NoLazy help me. Thank you for help very much!

svermeulen commented 6 years ago

Oh ok great, was just about to try and reproduce it. NonLazy should have fixed it though