Search Unity

OnNetworkSpawn not called

Discussion in 'Netcode for GameObjects' started by Gubendran_Vins, Oct 15, 2022.

  1. Gubendran_Vins

    Gubendran_Vins

    Joined:
    Mar 20, 2018
    Posts:
    62
    Hi,
    The problem is that the OnNetworkSpawn is never called for client only but whenever i called
    NetworkManager.Singleton.StartClient(); but it would be invoked when call
    NetworkManager.Singleton.StartHost();

    it has inherited from network behaviour.

    it has attached the network object.

    Please guide to what i missed on that.

    Thanks in advance.
     
  2. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    664
  3. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,969
    Please provide more info. How is the object spawned? By script or in-scene? Could you share the relevant code fragments?

    Personally i have not had this issue in both server side spawned objects and in-scene placed ones.
    The linked github issue is also lacking information.
     
  4. Gubendran_Vins

    Gubendran_Vins

    Joined:
    Mar 20, 2018
    Posts:
    62
    Hi @CodeSmile ,


    public override void OnNetworkSpawn() {
    Debug.Log("On NetworkSpawn");
    //if (IsServer || IsClient) {
    NetworkManager.Singleton.OnClientConnectedCallback += OnClientConnectedCallback;
    _playersInLobby.Add(NetworkManager.Singleton.LocalClientId, false);
    UpdateInterface();
    //}

    // Client uses this in case host destroys the lobby
    NetworkManager.Singleton.OnClientDisconnectCallback += OnClientDisconnectCallback;


    }

    The above function has been called while start server host has been called but this callback never called for while client join.

    This script has inherited from network behaviour script and also this gameobject has attached the network object component.

    Please guide me to fix this issue.
     
  5. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    229
    How do you spawn the network object? Is it already in the scene or are you spawning it dynamically?
     
  6. Gubendran_Vins

    Gubendran_Vins

    Joined:
    Mar 20, 2018
    Posts:
    62
    I have spawn dynamically from network prefabs.
     
  7. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    664
    Is the object actually spawning on the client, in the inspector does it have a networkId? On the server object in the inspector is the clientId listed under Observers?
     
  8. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,969
    Could you post the spawn code please?
     
  9. wondermagic

    wondermagic

    Joined:
    Jul 14, 2017
    Posts:
    5
    This may help someone else:

    In my case, I realized I had a duplicate NetworkManager and the NetworkObject was part of the duplicate. Of course, this duplicate NetworkManager was never started (because StartHost() or StartClient() was never called on it).

    To confirm, inspect the NetworkObject component of the object in question and check the NetworkManager field. Confirm that it is assigned to the same NetworkManager that you have started
     
    Kirby_S likes this.
  10. Kirby_S

    Kirby_S

    Joined:
    Apr 9, 2020
    Posts:
    3
    Thank you so much! you saved my day
     
    wondermagic likes this.
  11. RiverExplorer

    RiverExplorer

    Joined:
    Jul 28, 2021
    Posts:
    21
  12. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,969
    Just to point out a common oversight: Scene Management.

    One critical aspect is that you have to load scenes with NetworkManager.Singleton.SceneManager.
    The other issue arises when you don‘t change scenes at all. It‘s best to set up any Netcode project so that you have two scenes: pregame and ingame. Pregame has the NetworkManager. When starting host or server, you should load the ingame scene in the next line of code.
    When the client starts in pregame, it will automatically be transferred to ingame once connected.
    This guarantees that the Ingame scene and everything in it is fully networked.

    And of course upon disconnect you change back to the pregame scene.

    Then build your scene flow around that. Personally I am finding it exceptionally beautiful to stick with just those two scenes, and load all remaining content both for pregame (menu) and ingame (gui, map, etc) with additive scene loading.