Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by EmmetOT · Jan 08, 2020 at 11:29 AM · frameworkdependenciesdependency injection

Zenject Issue: Declaring Factory for Object which doesn't have all its dependencies yet

Hey, I have a specific situation where I have a state machine which I want to be global and scene independent. I want to be able to create new instances of states, states can be scene-dependent (such as a 'combat encounter state') and these scene-dependent states will have dependencies which exist only in that scene (such as 'enemies in the combat encounter'.)


Because I want to create new state objects, I have factories set up for each state class, and contained in a global state factory class which the state machine object has a dependency on. However because this factory class is bound in the start scene, and therefore in a different context than the combat encounter scene, none of the scene-specific bindings are bound correctly when I try to create an instance of the combat encounter state.

Here's a simple example with all the state machine complexity stripped out:

I have an empty MonoBehaviour, ObjectInGameScene, and a class ObjectWithDependency:


 public class ObjectWithDependency
 {
     private readonly ObjectInGameScene m_object;
 
     public ObjectWithDependency(ObjectInGameScene objectInGameScene)
     {
         m_object = objectInGameScene;
 
         Debug.Log("Created a dependency object. Object is null: " + (m_object == null), m_object);
     }
 
     public class Factory : PlaceholderFactory<ObjectWithDependency>
     {
 
     }
 }


I have a TestFactoryContainer class which just has a dependency on this object's factory:


 public class TestFactoryContainer
 {
     private readonly ObjectWithDependency.Factory m_factory;
 
     public TestFactoryContainer(ObjectWithDependency.Factory factory)
     {
         m_factory = factory;
     }
 
     public void Spawn()
     {
         m_factory.Create();
     }
 }


My scenes are set up so that one scene, Start, loads the other Game. Start contains the TestFactoryContainer class whereas the actual ObjectInGameScene object exists in Game. Usually I try binding the factory and factory container in an installer in the start scene, and binding the actual object in the game scene, but unsurprisingly this results in the following error:


 ZenjectException: Unable to resolve 'ObjectInGameScene' while building object with type 'ObjectWithDependency'. Object graph:
 ObjectWithDependency

because the ObjectWithDependency and the factory classes don't share a Container. I understand that the solution might involve using scene parenting or scene decorator contexts but I've been unable to get either to work. Or maybe this entire approach to using Zenject is flawed.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by JasonBennett · Jan 08, 2020 at 09:12 PM

Hi @EmmetOT,

I'm not familiar with Zenject, so YMMV with my help. That being said, I think I understand what you're trying to do, but it would be helpful if you map out the problem using real examples from your game so we can do communication error correction with concrete objects prior to conversing about the abstract ideas.

Please try to be as specific AND concrete as possible about how this is built and how you hope for it to work. I don't need to see code yet.

-Jason

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

118 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Best way to resolve dependencies 1 Answer

Getting on drag functions into other classes 1 Answer

how can use GUI.Button? framework don't recognize or suggest the method 1 Answer

Unity 5.3.1 and Visual Studio 2015: Project Target Framework Not Installed Error Message? 4 Answers

Unity 2018.3.0f2 Visual Basic Framework version error 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges