- Home /
Zenject injecting upon instantiation
Im using Zenject to try and inject code from my generic battlecard class to a specific battle card.
public class _GenericBattleCardBehaviour_ : ITest
{
int healthPool;
public void SetHealth(int health)
{
this.healthPool = health ;
Debug.Log(healthPool);
}
the test version of the specific battle card code
public class TestinjectorScript : MonoBehaviour
{
ITest _test;
[Inject]
public void Construct(ITest test)
{
this._test = test;
Debug.Log(_test);
}
void Start()
{
_test.SetHealth(10);
}
}
now when I load a scene with a card in the scene already its fine, but when I instantiate the battle card it wont even inject the code. Is it cause Im using a scene index vs a game object index? im very lost and the documentation is skimp.
Answer by Bunny83 · Jan 29, 2019 at 07:47 PM
Uhm, i haven't used zenject before, but have you read the documentation? You probably should read the General Guidelines / Recommendations / Gotchas / Tips and Tricks. The very first line reads:
Do not use GameObject.Instantiate [...] we recommend using a factory.
You should get familiar with the concepts of an injection framework. It should be clear that if you don't have any active code that does the injection, nothing will be injected "magically".
In the advanced topics there's an ZenAutoInjecter component which may help.
Note i've never used this framework and it took me less than 2 minutes to find those things in the documentation. You should spend more time researching and studying the documentation ;)
thanks for the reply, I did go through I just didnt see it even searched for the word instantiate just didnt see it.