unity-game-enginezenject

Extenject - NullReferenceException when second time inject


I'm new at Zenject(Extenject).

My dev environment: Win10, Unity2020, Extenject 9.2.0

Here is my question:

In installer bind the class

Container.Bind<AccountInfo>().AsCached();

Inject it at classA

private AccountInfo accountInfo;             

[Inject]
private void Init(GameSetup _gameSetup, AccountInfo _accountInfo)
{
    this.gameSetup = _gameSetup; 
    this.accountInfo = _accountInfo;
}

accountInfo.address = "xxx'; // works fine

Then inject AccountInfo to classB

private AccountInfo accountInfo;      
        
[Inject]
private void Init(AccountInfo _accountInfo)
{
    this.accountInfo = _accountInfo;
}

accountInfo.address = "xxx'; //NullReferenceException: Object reference not set to an instance of an object

Why accountInfo changed to null? AsCached() dosen't work? Or something worng else?

Help please~~ Thank you!

Here is my code:

Installer enter image description here

"ClassA" inject GameSetup, and create instance, works fine enter image description here

"ClassB" inject GameSetup, Error: null object enter image description here

"ClassB" Creator, I'm trying use container.Instantiate() to create it enter image description here


---update--- gameSetup still Null Object enter image description here


Solution

  • There are two cases, when injection will not work properly in your code.

    1. The code, that uses injected object is executed before Init. For example if this code is placed in the construcor.

    2. You create your GameObject/Component in runtime whithout using IInstantiator. While you use Znject you always should use IInstantiator to create objects. To do it you should inject IInstantiator to the object, that creates another objects. IItstantiator is always binded in the container by default, so you don't have to bind it manually. For example:

    
    public class Creator : MonoBehaviour {
    
        [SerializeField]
        private GameObject _somePrefab;
    
        private IInstantiator _instantiator;
    
        [Inject]
        public void Initialize(IInstantiator instantiator) {
            _instantiator = instantiator;
        }
    
        private void Start() {
    
            // example of creating components
            var gameObj = new GameObject(); // new empty gameobjects can be created without IInstantiator
            _instantiator.InstantiateComponent<YourComponentClass>(gameObj);
    
            // example of instantiating prefab
            var prefabInstance = _instantiator.InstantiatePrefab(_somePrefab);
        }
    
    }