Search Unity

How to properly disconnect client

Discussion in 'Netcode for GameObjects' started by soloantares, Feb 20, 2022.

  1. soloantares

    soloantares

    Joined:
    Feb 13, 2022
    Posts:
    2
    When I check https://docs-multiplayer.unity3d.com/docs/components/networkmanager/index.html#disconnecting for disconnecting a client it says one should use
    Code (CSharp):
    1. NetworkManager.Singleton.StopClient();
    . The problem with this is that it doesnt exist... Then I found
    Code (CSharp):
    1. NetworkManager.Singleton.Shutdown();
    for disconnecting a client from the client side. Now I face the problem, when I want to reconnect the same client again, I get these errors:

    [Netcode] NetworkPrefab hash was not found! In-Scene placed NetworkObject soft synchronization failure for Hash: 1225154588!

    Failed to spawn NetworkObject for Hash 1225154588.

    [Netcode] NetworkPrefab hash was not found! In-Scene placed NetworkObject soft synchronization failure for Hash: 2733240570!

    Failed to spawn NetworkObject for Hash 2733240570.

    OverflowException: Reading past the end of the buffer

    What am I doing wrong?
     

    Attached Files:

  2. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    660
    I did a quick test and Shutdown worked as expected. Are there pre-existing network objects already in the scene? You might also want to check if there are any spawned network objects still existing on the client after it disconnects.
     
    soloantares likes this.
  3. LightFury001

    LightFury001

    Joined:
    May 22, 2021
    Posts:
    4
    Do you found any solution ? I am having the same problem. Yes there are no pre-existing network objects everything has been disposed.
     
  4. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    660
    It might be worth making a note of the hashes to see which network objects are causing the problem.

    I used your post as an excuse to tidy up some rather dubious disconnect code as I'd been ignoring the errors it was throwing. Clients are now able to connect and reconnect repeatedly without errors. My setup is likely different from yours though, all network objects are not visible by default and I show and hide them as necessary. It might be worth sharing what you're doing and I can see if I can replicate it.
     
    LightFury001 likes this.
  5. LightFury001

    LightFury001

    Joined:
    May 22, 2021
    Posts:
    4
    I have a GameObject which has a NetworkObject component attached with it and I need this GameObject throughout the game. When I disconnect, I destroy this GameObject and reInstantiate it when the player goes to the Menu scene. And in the client side I get the above errors while reconnecting. And the mechanism of instantiating works as it should that is the Object do get spawn properly according to me ofcourse ^^ but still I get those errors for this GameObject .
     
  6. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    660
    I've tried something like you've described, spawning a game object on client connect and despawn and destroy on disconnect. That works fine for repeated connects and disconnects, but it's probably too simplistic to emulate what you're doing. You may have to share your project to understand what's going on in your case.
     
    LightFury001 likes this.
  7. LightFury001

    LightFury001

    Joined:
    May 22, 2021
    Posts:
    4
    hello @cerestorm sorry for replying late. I have fixed this issue by removing the NetworkObject component from the GameObject (As it had a script that was derived from NetworkBehaviour class) that was causing the problem. And this GameObject is not the PlayerObject but a different object which I need throughout the game. Now it is working fine but I am still not able to understand why it was'nt working with the first method (When there was networkObject component attached to it, and when it was deriving from the NetworkBehaviour class........now it is deriving from the good old MonoBehaviour)) :) => This was The first method I am talking about => https://answers.unity.com/questions...nnect-a-client-from-the-server-properl-1.html
     
  8. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    660
    What you're doing looks similar to what I'm working on. I'm not sure I understand "have applied the singleton so these objects do not get destroyed upon scene loading.", are you using DontDestroyOnLoad or something else?

    I wouldn't expect the network object to be destroyed automatically unless it's flagged to do so when it's spawned. I call Destroy() on network objects once I want them removed completely, I'm assuming OnDestroy() hooks into that but I've not looked into it too deeply.

    Btw your link is broken, this looks to be the correct one https://answers.unity.com/questions/1889629/how-to-disconnect-a-client-from-the-server-properl.html
     
  9. LightFury001

    LightFury001

    Joined:
    May 22, 2021
    Posts:
    4
    Yes, I tried using the NetworkManager in the default way but when I went back to my offline scene from the lobby scene (when disconnected basically) I got two network managers instead of 1 That is why I applied the singleton onto it. And the errors I get refers to basically the prefab hash of the object which I want throughout the game but I am getting this error. ( This Object makes use of the networkBehaviour therefore have network object component attached to it )
     
  10. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    660
    Are you spawning the NetworkManager yourself, as having multiples of it sounds very strange. I have one sitting in the start scene already so I need not worry about instantiating it.
     
  11. PashDev

    PashDev

    Joined:
    Nov 6, 2021
    Posts:
    2
    try reassigning the NetworkManager's NetworkPrefabs and the Player Prefab
     
  12. liambilly

    liambilly

    Joined:
    May 13, 2022
    Posts:
    154
    so are you saying if i still have in scene networkobjects the shutdown wont work correctly
     
  13. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    660
    No just looking for anything unusual, what problem are you having?
     
  14. liambilly

    liambilly

    Joined:
    May 13, 2022
    Posts:
    154
    player data is not being cleaned properly after disconnecting, where when he reconnects the cliendid is increased instead of being recycled,its a bug i reported it