Search Unity

Resolved Netcode for GameObjects - KeyNotFoundException in client for non-player NetworkObjects

Discussion in 'Netcode for GameObjects' started by lcompton, Dec 27, 2021.

  1. lcompton

    lcompton

    Joined:
    Mar 31, 2018
    Posts:
    102
    I'm running into a problem with non-player network prefabs. The prefabs have an "empty" as their root and a simple 3d object beneath the root (sphere, cube, etc.). The root object contains the following components...
    • NetworkObject
    • NetworkTransform
    • Rigidbody
    • NetworkRigidbody
    The prefabs have been registered with the NetworkManager.

    I have tried adding prefabs instances to my scene via the editor and I have also tried spawning them via a component. In both cases, a KeyNotFoundException is thrown when I run from a client. The host works fine.

    Any idea why this might be happening? I read some other posts that mentioned KeyNotFoundExceptions being thrown if you spawn and immediately change ownership. My case is simpler. I'm not changing ownership. I'm just trying to add simple network prefabs to my scene.

    Details...
    • Unity 2021.2.7f1
    • Netcode for GameObjects 1.0.0-pre.3
    Here's an example of the exception...
    KeyNotFoundException: The given key '230' was not present in the dictionary.
    System.Collections.Generic.Dictionary`2[TKey,TValue].get_Item (TKey key) (at <31c0f51ac5a24a22ba784db24f4ba023>:0)
    Unity.Netcode.NetworkManager.TransportIdToClientId (System.UInt64 transportId) (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.0-pre.3/Runtime/Core/NetworkManager.cs:1244)
    Unity.Netcode.NetworkManager.HandleRawTransportPoll (Unity.Netcode.NetworkEvent networkEvent, System.UInt64 clientId, System.ArraySegment`1[T] payload, System.Single receiveTime) (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.0-pre.3/Runtime/Core/NetworkManager.cs:1304)
    Unity.Netcode.NetworkManager.OnNetworkEarlyUpdate () (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.0-pre.3/Runtime/Core/NetworkManager.cs:1150)
    Unity.Netcode.NetworkManager.NetworkUpdate (Unity.Netcode.NetworkUpdateStage updateStage) (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.0-pre.3/Runtime/Core/NetworkManager.cs:1125)
    Unity.Netcode.NetworkUpdateLoop.RunNetworkUpdateStage (Unity.Netcode.NetworkUpdateStage updateStage) (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.0-pre.3/Runtime/Core/NetworkUpdateLoop.cs:149)
    Unity.Netcode.NetworkUpdateLoop+NetworkEarlyUpdate+<>c.<CreateLoopSystem>b__0_0 () (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.0-pre.3/Runtime/Core/NetworkUpdateLoop.cs:172)

    [Netcode] Deferred messages were received for NetworkObject #59, but it did not spawn within 1 second.
    UnityEngine.Debug:LogWarning (object)
    Unity.Netcode.NetworkLog:LogWarning (string) (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.0-pre.3/Runtime/Logging/NetworkLog.cs:18)
    Unity.Netcode.NetworkSpawnManager:CleanupStaleTriggers () (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.0-pre.3/Runtime/Spawning/NetworkSpawnManager.cs:140)
    Unity.Netcode.NetworkManager:OnNetworkPostLateUpdate () (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.0-pre.3/Runtime/Core/NetworkManager.cs:1188)
    Unity.Netcode.NetworkManager:NetworkUpdate (Unity.Netcode.NetworkUpdateStage) (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.0-pre.3/Runtime/Core/NetworkManager.cs:1131)
    Unity.Netcode.NetworkUpdateLoop:RunNetworkUpdateStage (Unity.Netcode.NetworkUpdateStage) (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.0-pre.3/Runtime/Core/NetworkUpdateLoop.cs:149)
    Unity.Netcode.NetworkUpdateLoop/NetworkPostLateUpdate/<>c:<CreateLoopSystem>b__0_0 () (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.0-pre.3/Runtime/Core/NetworkUpdateLoop.cs:232)
     
  2. lcompton

    lcompton

    Joined:
    Mar 31, 2018
    Posts:
    102
    I did some additional investigation. I don't know the significance of the number, but I'm only able to include 41 non-player network objects. This is true if I manually add the instances to my scene or if I spawn them via the NetworkManager. Once I create 42, the KeyNotFoundException is thrown in the client.

    I'm going to look into distributing the spawning events across multiple frames to see if that makes a difference. Weird problem.
     
  3. lcompton

    lcompton

    Joined:
    Mar 31, 2018
    Posts:
    102
    I tried spreading out the prefab instance spawn calls in the server, but the exception is still thrown when there are 42 or more non-player network prefab instances. It looks like the problem has to do with client initialization.

    I've run out of things to try so I submitted a bug report. If it turns out that I'm doing something wrong, I'll post the solution here at a later date.
     
    Last edited: Dec 29, 2021
  4. Punnet_Red

    Punnet_Red

    Joined:
    Mar 7, 2020
    Posts:
    8
    Hello, I have the same issue and here are some additional details I noticed in my project:
    • I notice that for my project, the number in the error message "KeyNotFoundException: The given key '230' was not present in the dictionary." changes every time I restart Unity.
    • The number of prefabs that are 'allowed' to spawn also appears to be different every time I restart.
    • Some threads cite parenting as a potential cause for issues similar to ours - I have tried both nesting and unnesting the prefabs in the editor, and both show the same KeyNotFoundException error.
     
  5. lcompton

    lcompton

    Joined:
    Mar 31, 2018
    Posts:
    102
    Punnet_Red likes this.
  6. lcompton

    lcompton

    Joined:
    Mar 31, 2018
    Posts:
    102
  7. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    664
    I've been having similar issues with large numbers of network objects. I was spawning them in batches to get around the size issue but I removed this as I wasn't happy with it as a solution, and it seems it won't help for late joining or rejoining players. I'm a bit concerned that Netcode hasn't been designed to handle projects with potentially thousands of network objects. Hopefully Unity will come back with a response on this.
     
  8. lcompton

    lcompton

    Joined:
    Mar 31, 2018
    Posts:
    102
    Same here. Overall, I'm impressed with Netcode for GameObjects, but having a relatively low limit on server managed objects is a showstopper for me unfortunately. I'll post updates here.
     
  9. lcompton

    lcompton

    Joined:
    Mar 31, 2018
    Posts:
    102
    Just a quick followup that might help others. I've switched from the default transport (UNet) to the Unity Transport. It's scaling better than UNet, although I'm still hitting an upper limit. Here are some observations...

    UNet Transport: With default values, the client would fail at 42 network managed objects. By increasing the "Message Buffer Size", I was able to have about 200 server managed objects before running into a secondary problem related to transform interpolation in the client.

    Unity Transport: By fiddling with the three queue size parameters, I was able to get up to 400 server managed objects before the server started having problems (there were various messages related to queue sizes).

    So, the Unity Transport is better, but there's still a pretty low limit on server managed objects. It looks like UNet is deprecated, so Unity Transport is the way to go. On the bright side, it's still in development, so this limitation will hopefully be eliminated eventually.

    Instructions for installing the Unity Transport and the associated Netcode wrapper are found here...
     
    cerestorm likes this.
  10. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    664
    Thanks, I've used that transport before without much greater success but I think there was other things going on that were clouding the issue. I'll give it another go.
     
    lcompton likes this.