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.