Search Unity

Question Spawn method spawns object twice on client?

Discussion in 'Netcode for GameObjects' started by galactichyperstar, Aug 16, 2021.

  1. galactichyperstar

    galactichyperstar

    Joined:
    Jun 4, 2018
    Posts:
    11
    I call the code below, which works fine on host, but when another client joins, I get a duplicate key error.
    There are two objects spawned on the client when it joins (excluding the one already spawned from the host), and on the server it correctly spawns one.
    upload_2021-8-16_2-37-16.png

    Here is the error on the client:
    ArgumentException: An item with the same key has already been added. Key: 4
    System.Collections.Generic.Dictionary`2[TKey,TValue].TryInsert (TKey key, TValue value, System.Collections.Generic.InsertionBehavior behavior) (at <695d1cc93cca45069c528c15c9fdd749>:0)
    System.Collections.Generic.Dictionary`2[TKey,TValue].Add (TKey key, TValue value) (at <695d1cc93cca45069c528c15c9fdd749>:0)
    MLAPI.Spawning.NetworkSpawnManager.SpawnNetworkObjectLocally (MLAPI.NetworkObject networkObject, System.UInt64 networkId, System.Boolean sceneObject, System.Boolean playerObject, System.Nullable`1[T] ownerClientId, System.IO.Stream dataStream, System.Boolean readPayload, System.Int32 payloadLength, System.Boolean readNetworkVariable, System.Boolean destroyWithScene) (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Spawning/NetworkSpawnManager.cs:362)
    MLAPI.Messaging.InternalMessageHandler.HandleAddObject (System.UInt64 clientId, System.IO.Stream stream) (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Messaging/InternalMessageHandler.cs:271)
    MLAPI.NetworkManager.HandleIncomingData (System.UInt64 clientId, MLAPI.Transports.NetworkChannel networkChannel, System.ArraySegment`1[T] data, System.Single receiveTime, System.Boolean allowBuffer) (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Core/NetworkManager.cs:982)
    MLAPI.NetworkManager.HandleRawTransportPoll (MLAPI.Transports.NetworkEvent networkEvent, System.UInt64 clientId, MLAPI.Transports.NetworkChannel networkChannel, System.ArraySegment`1[T] payload, System.Single receiveTime) (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Core/NetworkManager.cs:875)
    MLAPI.NetworkManager.OnNetworkEarlyUpdate () (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Core/NetworkManager.cs:683)
    MLAPI.NetworkManager.NetworkUpdate (MLAPI.NetworkUpdateStage updateStage) (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Core/NetworkManager.cs:641)
    MLAPI.NetworkUpdateLoop.RunNetworkUpdateStage (MLAPI.NetworkUpdateStage updateStage) (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Core/NetworkUpdateLoop.cs:148)
    MLAPI.NetworkUpdateLoop+NetworkEarlyUpdate+<>c.<CreateLoopSystem>b__0_0 () (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Core/NetworkUpdateLoop.cs:171)
     
  2. luke-unity

    luke-unity

    Joined:
    Sep 30, 2020
    Posts:
    306
    Sorry I can't see anything wrong. Your code looks correct and the error seems to be an MLAPI bug. I think I remember people having similar issues before when they sent RPCs immediately after spawning an object.