Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Injection on instantiated Scriptable Object from Game Object Context Installer

Discussion in 'Scripting' started by a-t-hellboy, Sep 9, 2023.

  1. a-t-hellboy

    a-t-hellboy

    Joined:
    Dec 6, 2013
    Posts:
    180
    Hey guys, I'm stuck on something about Zenject/Extenject. Any one has any idea how I can fix that ?

    I have Player gameObject with Game Object Context which instantiate Character prefab by factory and Charcater has Game Object Context. In CharacterController which is a facade class on Character I instantiate a few Scriptable Objects in this way:

    Code (CSharp):
    1. private readonly DiContainer _container;
    2.  
    3. public ZenjectResourceFactory(DiContainer container)
    4. {
    5.     _container = container;
    6. }
    7. public Object Instantiate(Object @object)
    8. {
    9.     Object instance = Object.Instantiate(@object);
    10.     _container.QueueForInject(instance);
    11.     return instance;
    12. }
    In the Character Installer CharacterBehavior and CharacterPowerup are bound by Container.Bind<CharacterBehaviorContext>().AsSingle().NonLazy(); and Container.Bind<CharacterPowerupContext>().AsSingle().NonLazy();

    But the issue is CharacterBehavior and CharacterPowerup are not injected into those Scriptable Objects [Inject] methods.
     
  2. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,104
    If you are creating these instances after the initial install phase, then I believe using DiContainer.Instantiate instead of Object.Instantiate and QueueForInject would work.
     
  3. a-t-hellboy

    a-t-hellboy

    Joined:
    Dec 6, 2013
    Posts:
    180
    Thanks for the reply.
    Unfortunately it doesn't work either.
    Also as I noticed, actually DiContainer.Instantiate is Object.Instantiate + DiContainer.Inject and I think they are totally same things. But again, I've tested that and no success.
    From github doc:
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Did you need to make another thread about this? Mind you it was annoyingly hijacked by the usual suspects.

    Was there any attention paid to my suggestion of using plain C# classes? Instantiating scriptable objects is probably work you don't need to do.
     
  5. a-t-hellboy

    a-t-hellboy

    Joined:
    Dec 6, 2013
    Posts:
    180
    I made better description for the issue I have, so I made another topic.
    Yup, I read your suggestion but it is not related to my problem also my system is based on using Scriptable Objects for different behaviors for different characters and I don't have a plan to change it. Also I've noticed same issue on plain C# classes. Do you have Zenject experience btw ?
     
  6. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,104
    Yeah looks like it uses DiContainer.Inject internally - which is not DiContainer.QueueForInject. But if that didn't work either, then I guess there must be some other issue here.
     
  7. a-t-hellboy

    a-t-hellboy

    Joined:
    Dec 6, 2013
    Posts:
    180
    Yup, I've tested with Inject too. No success either.
     
    SisusCo likes this.