Search Unity

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

Bug Client can't connect twice to a game

Discussion in 'Netcode for GameObjects' started by lavagoatGG, Jul 27, 2023.

  1. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    226
    Hello,
    If I try to create a lobby, and join it from another device, Then from the other device to leave and join again It doesn't work and I get the following error on the server and nothing on the client:
    MissingReferenceException: The object of type 'NetworkObject' has been destroyed but you are still trying to access it.

    No network object spawns and the host doen't get a clientConnected callback.
    This worked before and I don't know what causes this bug.
    Can someone help me?
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,066
    Sounds like the typical issue of manually destroying the NetworkManager (don't do that!) or otherwise improperly shutting down networking, at least more than what should be done.

    Typical setup for networking should include a loading or launch scene that contains nothing but the NetworkManager and other stuff that needs to be initialized first, and that scene loads the next scene from Update() of a script (I think you cannot load a scene earlier). This prevents NetworkManager from being duplicated when you reload the scene, which is why some devs started to destroy the NetworkManager to kill the duplicate.
     
  3. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    226
    But if you don't destroy it then the moment you go back to the loading scene it is duplicated isn't it?
     
  4. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    226
    Instead of destroying it I can put it in a scene that I never go back to but in my game the network manager must be on the play scene unless I make some big changes. Is there any other solution? Can I remove the don't destroy on load from the NetworkManager?
     
  5. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    640
    You're free to destroy the network manager yourself, DontDestroyOnLoad just prevents it from being destroyed on a scene load. As CodeSmile said though, it's preferable to have the network manager in an earlier scene rather than having to destroy it.

    If you want to go the destroy route take a look at the network manager's OnServerStopped/OnClientStopped callbacks, keep in mind they're called before shutdown so you'll have to experiment with them to make sure you give the network manager enough time to shutdown and clean up.

    The callbacks were introduced in 1.4.0 so now might be a good time to update. :)
     
  6. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,066
    No one ever goes back to the loading scene. ;)

    I mean the loading scene that appears for a split second as the app launches, not the "level is loading" scene. That may be confusing, so let's call it the "launch scene". You never ever go back to that one!
     
  7. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    226
    The new versions use a prefab list outside the network manager and I can't put my prefabs there because of how I implemented it. I will try to wait until shutdownInProgress becomes false and then destroy it and see if it makes a difference.
     
  8. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    640
    Didn't the prefab list changes make it more flexible? It's up to you but I wouldn't want to be tied to an older version and not be able to take advantage of improvements and more importantly the fixes, unless your project is nearing completion.
     
  9. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    226
    I implemented the networkobjects when I wasn't so good with netcode and it seems like somehow I am spawning in-scene placed object dinamically. This means the prefabs on the manager's list must be the in scene placed objects, and the new system doesn't allow me to put them on the list. Also, I have to put the network manager in the same scene so I can drag the objects to it.
    My project is near completion and this problem with connection is the only thing that isn't working now.
    Destroying the network manager after it finished shutting off doesn't fix the problem and I still can't reconnect.
     
  10. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    640
    It's hard to say what the problem is, compare the network manager's configuration on connection and reconnection. Is StartClient returning true? It could be a timing issue but I'm just guessing really.
     
  11. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    226
    Okay so couple of things I found:
    1. The problem is in the server, if a client leaves then no client can join anymore
    2. This isn't related to destroying the networkManager because even if I don't destroy it on the leaving client no client can join.
    This is my disconnection code:

    public async void comeHome()
    {
    try
    {
    wantToComeHome = true;
    if (NetworkManager.Singleton.IsServer)
    {
    await LobbyService.Instance.DeleteLobbyAsync(lobbyId);
    }
    else
    {
    await LobbyService.Instance.RemovePlayerAsync(lobbyId, playerId);
    }
    Debug.Log("sucssefully dissconected from lobby");
    }
    catch (LobbyServiceException e)
    {
    Debug.Log(e);
    }
    //shutdown
    try
    {
    NetworkManager.Shutdown();
    //StartCoroutine(waitToShutdown());
    }
    catch
    {
    Debug.Log("Network manager already shut down");
    wantToComeHome = false;
    }

    }

    Then after that I wait to the shutdown to complete (destroy the network manager) and switch scenes.
     
  12. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    640
    Going back to the original error on the server:
    Code (CSharp):
    1. MissingReferenceException: The object of type 'NetworkObject' has been destroyed but you are still trying to access it.
    What object is this, is it the Player of the disconnecting client, can you see what's accessing it in the stacktrace?
     
  13. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    226
    This is the full error message:

    Code (CSharp):
    1. MissingReferenceException: The object of type 'NetworkObject' has been destroyed but you are still trying to access it.
    2. Your script should either check if it is null or you should not destroy the object.
    3. Unity.Netcode.SceneEventData.SortParentedNetworkObjects (Unity.Netcode.NetworkObject first, Unity.Netcode.NetworkObject second) (at Library/PackageCache/com.unity.netcode.gameobjects@1.2.0/Runtime/SceneManagement/SceneEventData.cs:364)
    4. System.Collections.Generic.ArraySortHelper`1[T].InsertionSort (T[] keys, System.Int32 lo, System.Int32 hi, System.Comparison`1[T] comparer) (at <890d6fe26e8c408ea64b353e791fafce>:0)
    5. System.Collections.Generic.ArraySortHelper`1[T].IntroSort (T[] keys, System.Int32 lo, System.Int32 hi, System.Int32 depthLimit, System.Comparison`1[T] comparer) (at <890d6fe26e8c408ea64b353e791fafce>:0)
    6. System.Collections.Generic.ArraySortHelper`1[T].IntrospectiveSort (T[] keys, System.Int32 left, System.Int32 length, System.Comparison`1[T] comparer) (at <890d6fe26e8c408ea64b353e791fafce>:0)
    7. System.Collections.Generic.ArraySortHelper`1[T].Sort (T[] keys, System.Int32 index, System.Int32 length, System.Comparison`1[T] comparer) (at <890d6fe26e8c408ea64b353e791fafce>:0)
    8. Rethrow as InvalidOperationException: Failed to compare two elements in the array.
    9. System.Collections.Generic.ArraySortHelper`1[T].Sort (T[] keys, System.Int32 index, System.Int32 length, System.Comparison`1[T] comparer) (at <890d6fe26e8c408ea64b353e791fafce>:0)
    10. System.Collections.Generic.List`1[T].Sort (System.Comparison`1[T] comparison) (at <890d6fe26e8c408ea64b353e791fafce>:0)
    11. Unity.Netcode.SceneEventData.AddSpawnedNetworkObjects () (at Library/PackageCache/com.unity.netcode.gameobjects@1.2.0/Runtime/SceneManagement/SceneEventData.cs:248)
    12. Unity.Netcode.NetworkSceneManager.SynchronizeNetworkObjects (System.UInt64 clientId) (at Library/PackageCache/com.unity.netcode.gameobjects@1.2.0/Runtime/SceneManagement/NetworkSceneManager.cs:1575)
    13. Unity.Netcode.NetworkManager.HandleConnectionApproval (System.UInt64 ownerClientId, Unity.Netcode.NetworkManager+ConnectionApprovalResponse response) (at Library/PackageCache/com.unity.netcode.gameobjects@1.2.0/Runtime/Core/NetworkManager.cs:2274)
    14. Unity.Netcode.ConnectionRequestMessage.Handle (Unity.Netcode.NetworkContext& context) (at Library/PackageCache/com.unity.netcode.gameobjects@1.2.0/Runtime/Messaging/Messages/ConnectionRequestMessage.cs:165)
    15. Unity.Netcode.MessagingSystem.ReceiveMessage[T] (Unity.Netcode.FastBufferReader reader, Unity.Netcode.NetworkContext& context, Unity.Netcode.MessagingSystem system) (at Library/PackageCache/com.unity.netcode.gameobjects@1.2.0/Runtime/Messaging/MessagingSystem.cs:511)
    16. Unity.Netcode.MessagingSystem.HandleMessage (Unity.Netcode.MessageHeader& header, Unity.Netcode.FastBufferReader reader, System.UInt64 senderId, System.Single timestamp, System.Int32 serializedHeaderSize) (at Library/PackageCache/com.unity.netcode.gameobjects@1.2.0/Runtime/Messaging/MessagingSystem.cs:384)
    17. UnityEngine.Debug:LogException(Exception)
    18. Unity.Netcode.MessagingSystem:HandleMessage(MessageHeader&, FastBufferReader, UInt64, Single, Int32) (at Library/PackageCache/com.unity.netcode.gameobjects@1.2.0/Runtime/Messaging/MessagingSystem.cs:388)
    19. Unity.Netcode.MessagingSystem:ProcessIncomingMessageQueue() (at Library/PackageCache/com.unity.netcode.gameobjects@1.2.0/Runtime/Messaging/MessagingSystem.cs:404)
    20. Unity.Netcode.NetworkManager:OnNetworkEarlyUpdate() (at Library/PackageCache/com.unity.netcode.gameobjects@1.2.0/Runtime/Core/NetworkManager.cs:1600)
    21. Unity.Netcode.NetworkManager:NetworkUpdate(NetworkUpdateStage) (at Library/PackageCache/com.unity.netcode.gameobjects@1.2.0/Runtime/Core/NetworkManager.cs:1532)
    22. Unity.Netcode.NetworkUpdateLoop:RunNetworkUpdateStage(NetworkUpdateStage) (at Library/PackageCache/com.unity.netcode.gameobjects@1.2.0/Runtime/Core/NetworkUpdateLoop.cs:185)
    23. Unity.Netcode.<>c:<CreateLoopSystem>b__0_0() (at Library/PackageCache/com.unity.netcode.gameobjects@1.2.0/Runtime/Core/NetworkUpdateLoop.cs:208)
    24.  
    I am not sure which object causes the issue but it's in this code:

    Code (CSharp):
    1. /// <summary>
    2.         /// Sorts the synchronization order of the NetworkObjects to be serialized
    3.         /// by parents before children.
    4.         /// </summary>
    5.         /// <remarks>
    6.         /// This only handles late joining players. Spawning and nesting several children
    7.         /// dynamically is still handled by the orphaned child list when deserialized out of
    8.         /// hierarchical order (i.e. Spawn parent and child dynamically, parent message is
    9.         /// dropped and re-sent but child object is received and processed)
    10.         /// </remarks>
    11.         private int SortParentedNetworkObjects(NetworkObject first, NetworkObject second)
    12.         {
    13.             // If the first has a parent, move the first down
    14.             if (first.transform.parent != null)
    15.             {
    16.                 return 1;
    17.             }
    18.             else // If the second has a parent and the first does not, then move the first up
    19.             if (second.transform.parent != null)
    20.             {
    21.                 return -1;
    22.             }
    23.             return 0;
    24.         }
     
  14. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    640
    Yeah, it's possible either first or second is null. I compared the HandleConnectionApproval code for 1.5.1 and 1.2.0, it's been moved around a bit but looks essentially the same but I can't say for sure.

    The only issue similar to yours I've found is this one, it might be worth messaging him to see if he found a fix for the problem.
     
  15. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    226
    According to the line number, fist is null.
    I also found this similar but it is a bit old. I will try anyway.
     
  16. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    226
    If I upgrade to netcode 1.5.1 the client is able to rejoin but I am not sure this is optimal because I get the following message on my network prefab list
    upload_2023-8-9_19-19-24.png
    and if I migrate it nothing works.
     
  17. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    640
    The old prefab list in the network manager has been changed to support scriptable objects containing their own list. There should be a scriptable object called NetworkPrefabs somewhere in your project with a reference to it in the network manager, click on the object list in the network manager to find where it is and check it's populated.
    upload_2023-8-9_19-39-19.png
    If you're using ParrelSync you might have to do the same in the clones if it allows it, if it doesn't delete and recreate the clones.
     
  18. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    226
    Yes but if I do that it says missing refrence on all my in-scene placed networkObjects and I can't assign them to the list.
     
  19. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    640
    Why do you need to add them to the prefab list, shouldn't in-scene objects be spawned automatically? I vaguely remember trying to tie in-scene and prefab list objects together and I couldn't get it to work outside the network manager scene.
     
  20. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    226
    I really don't know. All I know is I get errors if the objects aren't on the network prefab list. I am not switching scenes so that's okay
     
  21. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    640
    What errors are you seeing? I don't think the prefab list is that fussed about what prefabs you add to it, what components are on your prefabs?
     
  22. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    226
    upload_2023-8-11_8-22-12.png
    This is the list I get after migrating.
    If I try to build and run the game I get the following errors:
    upload_2023-8-11_8-28-12.png
    If I assign my prefabs to the list I get the same errors.
    Previously I lost those errors by dragging the in-scene object itself to the list and then It could be spawned but the new list doesn't allow me to put in-scene object into it.
    It only works if I use the old list to assign the prefabs and then switch to the new version of netcode and don't migrate the list.
     
  23. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    640
    I'd leave the prefab list for now and try and work out why the in-scene objects aren't spawning when the network is started. Try creating a new in-scene network object and check it's being assigned a NetworkObjectId on StartServer/Host. It's handy to have a separate test project for this sort of thing.