OnClientConnected not working when Hosting after shutting down unsuccessful client

OnClientConnected callback is not being called when:

  1. StartClient, no host meaning no connection, manual shutdown
  2. Start a host, i can see that ConnectionApproval is happening, but OnClientConnected is not being called meaning i can not start my host properly.
    Is this a bug?
    When i start a host without first starting a client both ConnectionApproval and OnClientConnected callbacks are working properly.

I am doing this on the same instance of the game, not two different instances.

Naturally this won’t call OnClientConnected because the client never connected to a server/host. :wink:

Post your code.

You may have registered for the events AFTER you started the client/host. That may miss some of the initial events.

If by that you mean you are starting both server/host AND a client in the same instance, that won’t work. Should actually print an error message, something like “session already started” or similar.

Naturally this won’t call OnClientConnected because the client never connected to a server/host. :wink:

I know, this is not a problem. I need to get OnClientConnected when the same instance is shutdown and then successfully tries to be a host.

If by that you mean you are starting both server/host AND a client in the same instance, that won’t work. Should actually print an error message, something like “session already started” or similar.

What i meant by that is i am doing the chain of actions on the same instance of the game, instead of hosting on one instance and connecting on another.

You may have registered for the events AFTER you started the client/host. That may miss some of the initial events.

Okay, this gave me an idea to check.
This is how i register events. It seems that when i do NetworkManager.Singleton.StartClient();, then shut it down with NetworkManager.Singleton.Shutdown();, and then try to start a host NetworkManager.Singleton.StartHost();, OnNetworkSpawn is not being called, even though it should.
it is not called when i StartClient, since it is not connecting to the host, and then not called when i StartHost.

    public override void OnNetworkSpawn()
    {
        NetworkManager.OnClientConnectedCallback += ClientConnected;
        NetworkManager.OnClientDisconnectCallback += ClientDisconnected;

        base.OnNetworkSpawn();
    }

Is this expected behaviour or is it a bug?

EDIT: Okay it seems that the script that has OnNetworkSpawn is being deleted when i Shutdown(). I will add more once i investigate further.

EDIT2: Okay, the problem is that unity is forcing me to add NetworkObject on the gameObject that has logic for OnNetworkSpawn, upon shutdown that object is being destroyed which is causing the problems.
What is the right way to tackle this issue?

So, the solution is to move the NetworkLogic to an object without NetworkObject and make sure it is not a NetworkBehaviour. This way it will still be present when i shutdown the client.

One thing to keep in mind is that when i host and then shutdown, the network objects are not getting deleted, only when i shutdown as a client they are gone.

So when you call Shutdown() as the host you should know that this is not instant! You have to wait for the IsServer and IsListening flags to become false before starting another session.

This is registering these events too late since spawning doesn’t occur until after a client has connected. You need to subscribe to NetworkManager events BEFORE you call StartClient/StartHost!

So when you call Shutdown() as the host you should know that this is not instant! You have to wait for the IsServer and IsListening flags to become false before starting another session.

Yes, i do exactly that before starting the new host.

This is registering these events too late since spawning doesn’t occur until after a client has connected. You need to subscribe to NetworkManager events BEFORE you call StartClient/StartHost!

Yes, which is why i ditched network object and made the network logic in mono behaviour to make sure that it is not getting deleted with Shutdown.

Can you tell me why when shutdowning as ahost my in-scene placed network objects stay in place, but when i shutdown as a client these in-scene placed network objects are removed?

It seems logical that an in-scene placed network object remains since objects don’t just get deleted from a scene.

From the client’s perspective this may be different because the client will automatically load the currently networked scene when connecting. From the client’s perspective these objects are no longer networked and then removed. The client is supposed to leave the scene when disconnecting.

I suppose you have a setup where you don’t actually change scenes when networking? Best practice is to load a scene after StartHost and upon disconnect or shutdown, load a non-networked scene for both server and client (eg menu).

I suppose you have a setup where you don’t actually change scenes when networking? Best practice is to load a scene after StartHost and upon disconnect or shutdown, load a non-networked scene for both server and client (eg menu).

I have a lobby scene where players can host and connect as clients, they also can just press disconnect button to go back to the menu, all in one scene. When they disconnect i shutdown the player. Since the menu has certain networked logic i was using network behaviour objects which made it impossible to Host/Connect, Disconnect and Host/Connect again since NGO removes these in-scene obects when connected as a client, but not as a host, which i find weird.

I solved these issues, just like i said above, by making sure all networked objects (which are responsible for networked logic) are spawned dynamically.

I wasn’t aware of this and I would have thought this would impact references wired up in the inspector but apparently it doesn’t and the references are restored when a destroyed game object is re-created during scene load. At least that’s what testing and ChatGPT tells me, I wonder if it’s always guaranteed.

It looks like both the host and client can use the OnClientStopped event to know when shutdown is complete.

1 Like

It shouldn’t remove the in-scene placed NetworkObjects on the client side if scene management is enabled. If scene management is disabled, then it will destroy any pre-loaded in-scene placed NetworkObjects on clients as the initial synchronization will treat those as dynamically spawned.

1 Like

You’re correct and I should have tested this more thoroughly. In-scene objects aren’t destroyed on the client when scene management is enabled. Unique-Player if you’re seeing something different let us know.

I just tested on an empty scene and it still destroys NetworkObjects when i shutdown the client.
To replicate the issue:

  1. Create an empty scene
  2. Add two gameobjects. One with NetworkObject, another with NetworkManager and Unity Transport
  3. Start the scene, start the client via NetworkManager (buttons are in the inspector)
  4. Stop the client, observe how NetworkObject is destroyed

Scene Management is on.

NGO 1.9.1
Unity 2022.3.30f1

I’ve tried to reproduce this with NGO 1.9.1 and Unity 2022.3.55f1 but the in-scene object is never destroyed. Can you provide your project so I can try it?

Create a new project, add NGO, add this package, open the scene, run it, start client, stop client
BuiltIn-2022.3.30f1.unitypackage (3.2 KB)

Ah I see what you mean now, I should have spotted this sooner. This issue was fixed some time after 1.9.1, updating to the latest NGO version will resolve this.

For the current unity LTS version, 1.9.1 is the latest version of NGO. Does that mean only unity 6 will resolve this issue?

No you can update the version manually. Open the project/Packages/manifest.json file and change the NGO entry to "com.unity.netcode.gameobjects": "1.12.0"