Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Disconnecting as a client

Discussion in 'Netcode for GameObjects' started by lavagoatGG, Oct 7, 2022.

  1. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    226
    Hello,
    In my game, when a match ends, a client RPC is called that says to all clients to open a certain canvas. In that canvas, they have a button that when pressed will disconnect them from netcode and bring them back to the main menu scene.
    In order to disconnect them from netcode, I am using NetworkManager.Singleton.Shutdown(); as it says I should in the documentation(https://docs-multiplayer.unity3d.co...er/index.html#disconnecting-and-shutting-down)
    My problem is that when I call this function, and then change the scene, I get the following error 3 times:

    NotServerException: Destroy a spawned NetworkObject on a non-host client is not valid. Call Destroy or Despawn on the server/host instead.
    Unity.Netcode.NetworkObject.OnDestroy () (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.1/Runtime/Core/NetworkObject.cs:440)

    In my scene, I have 3 in-scene placed network objects. 2 of them are inactive in another canvas. the third one is active.
    When I try to connect to another game on the same device, I get the following errors:
    [Netcode] Failed to create object locally. [globalObjectIdHash=1194948114]. NetworkPrefab could not be found. Is the prefab registered with NetworkManager?

    (The network object is registered in the networkManager prefab list)

    Failed to spawn NetworkObject for Hash 1194948114.
    OverflowException: Reading past the end of the buffer


    Only one of the 3 network objects is active when trying to join again.

    Also, When tying to join, I get 3 warnings that says that a network prefab can't be null.
    Additionally, I get 2(!) network managers, one of them with a started client, the other without a started client and all of the network objects are on his prefabs list.


    What am I doing wrong? How should I disconnect the client?

    Thank you.
     
    Last edited: Oct 7, 2022
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,107
    Try sending an RPC to the server that then disconnects the client. Just an idea.
     
  3. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    226
    I will try it
     
  4. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    226
    It is still not working. I get the same errors+now the host can't start a new game properly. Maybe the scene changes before the network manager disconnects the client? NetworkManager.Singleton.DisconnectClient(id) isn't await so how can I wait for it to happen?
     
  5. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    647
    Try hooking into the disconnect on the client side using NetworkManager.Singleton.OnClientDisconnectCallback, and have something like the following in the callback method:
    Code (CSharp):
    1.     private void OnClientDisconnectCallback(ulong clientId)
    2.     {
    3.         Debug.Log("NetworkCallbackService OnClientDisconnectCallback: " + clientId);
    4.  
    5.         NetworkManager networkManager = GameObject.FindObjectOfType<NetworkManager>();
    6.  
    7.         GameObject.Destroy(networkManager.gameObject);      
    8.  
    9.         SceneManager.LoadScene("StartScene");
    10.     }
    Destroying the NetworkManager seems to work but I don't know if it's recommended, I just tried it for testing.
     
    lavagoatGG likes this.
  6. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    226
    Isn't the NetworkManager.Singleton.OnClientDisconnectCallback called when every client dissconnects and not only the local client?
     
  7. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    226
    When I delete the network manager I can connect again! Thank you very much!
    I still get
    NotServerException: Destroy a spawned NetworkObject on a non-host client is not valid. Call Destroy or Despawn on the server/host instead.

    errors when leaving to the start scene
     
  8. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    647
    Only on the server side, the passing of the clientId is likely due to the client and server sharing the same callback and only has meaning on the server.

    I would have thought the NetworkManager would clean them up but it sounds like they're left behind, is that only the in-scene ones?
     
    lavagoatGG likes this.
  9. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    226
    I am using only in scene network objects.
     
  10. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    647
    I'm not sure about them, unless there's something in the documentation that states they need to be handled differently it could be a bug.

    I wasn't able to get in-scene objects working when I tried, at least not ones that weren't in the same scene as the network manager. If you have no luck I'll have another try a bit later.
     
  11. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    226
    They are in the same scene as the network manager. If I want to use spawned network objects instead, what should I do to make them spawn when a client joins?
     
  12. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    647
    Are you using DontDestroyOnLoad to preserve them across scenes? It doesn't sound like the case as they'd also be left on the client in the same way as the network manager.

    Using NetworkManager.Singleton.OnClientConnectedCallback on the server would be one place to get things started.
     
    Last edited: Oct 7, 2022
  13. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    226
    I left the don't destroy on load on the default option
     
  14. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    647
    What option is that, I call DontDestroyOnLoad() on an attached script. I'm a bit confused as the error suggests the objects are being destroyed which is what DontDestroyOnLoad should prevent.
     
  15. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,107
    Just to be sure: after you shutdown the server, how do you change the scene?

    According to the docs, you cannot use the NetworkManager's SceneManager anymore, you have to load that scene using regular Unity SceneManagement calls.
     
  16. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    226
    Yes I am calling the regural scene managment.
    SceneManager.LoadScene(0);
     
  17. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,107
    And do you call NetworkManager.Singleton.Shutdown() on the clients as well?
    They need to perform cleanup too.
     
    Doraemon231 likes this.
  18. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    226
    I called DontDestroyOnLoad() on every network object and I don't get errors anymore. Thanks!
     
    Isnizal likes this.