0

I am new to Zenject and I am trying to bind an interface to a MonoBehaviour like below. In this case, I am trying to simulate to play sound inside Player.

using UnityEngine;
using Zenject;

public class GameInstaller : MonoInstaller
{
    public override void InstallBindings()
    {
        Container.Bind<IAudioService>().To<AudioController>().AsSingle();
    }
}
using UnityEngine;
using Zenject;

public class Player : MonoBehaviour
{
    [Inject] private IAudioService _audioService;

    private void Start()
    {
        _audioService.Play();
    }
}
using UnityEngine;

public class AudioController : MonoBehaviour, IAudioService
{
    public void Play()
    {
        Debug.Log("Audio called");
    }
}

But, I get errors saying "ZenjectException: Assert hit! Error occurred while instantiating object of type 'AudioController'. Instantiator should not be used to create new mono behaviours. Must use InstantiatePrefabForComponent, InstantiatePrefab, or InstantiateComponent."

For some reason it does not work. But, when I change AudioController to a Non-MonoBehaviour class it works fine. Any idea why?

I was expecting it to work.

  • First question coming to mind is why are you needing zenject? – BugFinder Aug 28 '23 at 14:52
  • @BugFinder dependency injection ^^ – derHugo Aug 28 '23 at 14:55
  • As the error is telling you using this kind o dependency injection on `MonoBehaviour` components is a bit tricky ... it can't simply use `new` on those so where should the instance be combing from? -> [`Must use InstantiatePrefabForComponent, InstantiatePrefab, or InstantiateComponent.`](https://github.com/modesttree/Zenject#dicontainerinstantiate) – derHugo Aug 28 '23 at 14:56
  • What is the proper way of binding an interface to a MonoBehaviour? I tried the ways mentioned in the error. But, they didn’t seem to work either. – Ahmet Altioglu Aug 28 '23 at 14:59
  • I think you basically have to use one of the `FromXYZ` extensions to define where the instance should be coming from rather then using the default behavior (`new`) – derHugo Aug 28 '23 at 15:05
  • i know what zenject is, but so many people say its not necessary in games.. i was wonderring if there was another way to do it without – BugFinder Aug 28 '23 at 15:14
  • I did this example for learning purposes only. I am not making a game or whatsoever. I am just learning Zenject. – Ahmet Altioglu Aug 28 '23 at 15:15
  • @BugFinder it is not really related to the type of project you are working on (games or not) but rather about general programming principles and your favorite application architecture design ... you can completely live without Zenject in specific and implement the DI yourself as well ;) .. you can also live without DI and use other means to access your references ... mainly opinion based ;) If you use `AsSingle` you could as well use Singleton for instance or here `FindObjectOfType` – derHugo Aug 28 '23 at 15:29
  • @derHugo yes, but given our OP is new to it, i was wondering if it becomes a doing it because doing it, not necessariy because its necessary – BugFinder Aug 28 '23 at 15:36

0 Answers0