OnNetworkSpawn not called

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.

There’s a Github issue reporting this, in case you want to add to it: Onnetwork spawn callback not working · Issue #2243 · Unity-Technologies/com.unity.netcode.gameobjects · GitHub

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.

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.

How do you spawn the network object? Is it already in the scene or are you spawning it dynamically?

I have spawn dynamically from network prefabs.

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?

Could you post the spawn code please?

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

1 Like

Thank you so much! you saved my day

1 Like

I have the same problem: Here is the code I used: Client-server quickstart for Netcode for GameObjects | Unity Multiplayer Networking

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.

1 Like