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. We’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. Dismiss Notice

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:
    57
    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:
    584
  3. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,578
    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:
    57
    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:
    215
    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:
    57
    I have spawn dynamically from network prefabs.
     
  7. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    584
    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:
    3,578
    Could you post the spawn code please?
     
  9. wondermagic

    wondermagic

    Joined:
    Jul 14, 2017
    Posts:
    3
    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.