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.

Question Zenject+Netcode spawn problem

Discussion in 'Netcode for GameObjects' started by Balphagore, Feb 28, 2023.

  1. Balphagore


    Jul 18, 2019
    Hi all. I am trying to share Zenject and Netcode for Gameobjects, but I ran into problem. To correctly inject dependencies of instantiated objects in Zenject, i need to spawn the prefab not through GameObject.Instantiate, but with the help of

    Code (CSharp):
    1. GameObject instance = diContainer.InstantiatePrefab(characterPrefab);
    Then all the dependencies of this perfab that use [Inject] will be written correctly.

    But the NetworkObject is spawned on the server using the Spawn() command. And on the client, a copy of this object is simply created from the server without the ability to instantiate it through diContainer. And dependencies written with [Inject] are not created.

    How in Zenject reinject dependencies for objects that for some reason already exist in the scene and were not created using diContaner? For example, like these NetworkObjects on client created by the server. Or is it possible to somehow override how objects are created on the client so that they use diContainer?
  2. mishakozlov74


    Aug 8, 2018
  3. Karabin


    Apr 26, 2015
    I have the same trouble. I can't inject dependencies to instantiated NetworkObjects. And registering INetworkPrefabInstanceHandler does not work in the host mode. Documentation says that I have to use RegisterHostGlobalObjectIdHashValues instead handlers. But this cannot help me with injection.
    I have a partial solution. I created MonoBehaviour which injects its gameObject in Awake method and attached this MonoBehaviour to each GameObject. But there is unresolved question how to specify injecting context right.