- Home /
How do I inject dependencies into a Player prefab instantiated by the Network Manager?
Hi everyone!
Recently I've started using Zenject as a dependency injection framework.
I'm developing a multiplayer game with a dedicated server and 2 players.
When a player connects to the server after clicking LAN Client(C) on Network Manager HUD, the player prefabs are instantiated on clients and the server. Problem is that the player prefab has no dependencies injected in the prefab components (all fields marked with [Inject] attribute remain empty)
How do I change that?
Answer by eventropy · Jan 10, 2018 at 10:50 AM
The easiest way to fix this would be to install the latest version of Zenject and then attach a ZenAutoInjector component to the root of the prefabs. This will cause the prefab to be injected when it is instantiated. The only draw back is that it assumes that you want to use the SceneContext container for injection, so you can't use this method if you want it to be injected from within a subcontainer.
Also, because this is a new feature, it hasn't been included in a release yet so you have to install zenject by copying directly from the github repo. It will be included in the next release (5.5 or maybe 5.4.1)