#[Zenject] Issues - Can't [Inject] MonoBehaviours In Scene Context, please HELP!

5 messages · Page 1 of 1 (latest)

gritty ore
#

So I am trying to bind multiple instances of **MonoBehaviours **and **ScriptableObjects **in a SceneContext, so they stay available during the gameplay context.

But the [Inject]-Flag doesn't grab the bound Instances...

public class GameInstaller : MonoInstaller<GameInstaller>
    {
        [SerializeField] private CharacterCreator characterCreator;
        [SerializeField] private AbilityDatabase abilityDatabase;
        [SerializeField] private SentinelClassDatabase sentinelClassDatabase;
        [SerializeField] private ItemDatabase itemDatabase;
        [SerializeField] private GameManager gameManager;
        [SerializeField] private CameraManager cameraManager;

        public override void InstallBindings()
        {
            Container.BindInstance(abilityDatabase).AsSingle();
            Container.BindInstance(sentinelClassDatabase).AsSingle();
            Container.BindInstance(itemDatabase).AsSingle();
            Container.BindInstance(this).AsSingle();
            Container.Bind<CharacterManager>().WithId("local").To().AsSingle();
            Container.BindInstance(cameraManager).AsSingle();
        }
    }

Here are some example usages of the [Inject] Flag.

public class CharacterManager : CharacterComponent
    {
...
[Inject]
        private CameraManager _cameraManager;
[Inject(Id = "local")]
        private CharacterManager _localCharacterManager;

...
public void Start()
{
//Here I want to set the bound localCharacterManager to this very CharacterManager instance but that also doesn't work.
  _localCharacterManager = this;
}
public class UIManager : FiniteUIStateMachineMono
    {
[Inject]
        private static CameraManager _cameraManager;

public static Vector3 ToScreenPointPosition(Vector3 targetVector)
        {
            return _cameraManager.brainCamera.OutputCamera.WorldToScreenPoint(targetVector);
}

But these [Inject]-ed references always stay null.

#

But these [Inject]-ed references always stay null.

Any idea what I am missing? Tried *factories *and other ways to bind MonoBehaviours. Nothing did the trick...

Help, I'm about to stop using Zenject because I'm trying since 2 weeks, with no luck. 😦

atomic gust
#

i will never understand why anyone tries to use DI for unity game development

#

just make a singleton

gritty ore
#

Bump