Search Unity

Feedback Spawn NetworkObject in runtime

Discussion in 'Netcode for GameObjects' started by firaui, Jul 12, 2021.

  1. firaui

    firaui

    Joined:
    Nov 14, 2018
    Posts:
    12
    Hello,

    I'm working with MLAPI from v0.1.0 and now want to upgrade to develop branch. I have some difficulty when spawning network object in runtime. In v0.1.0 you can get Prefab object from NetworkConfig, but in develop branch the API is changed.

    I already tried somethings:
    1. Get NetworkObject from NetworkPrefabs variable in NetworkConfig. In develop branch the accessor is changed to internal so I cannot access it.
    2. Move the NetworkObject to Addressable, load in runtime and spawn. Server can spawn these object, but error in Client. After further investigation because these object is from addressable, the GlobalObjectIdHash is different from the NetworkConfig so client throws these error

    Code (CSharp):
    1. [MLAPI] Failed to create object locally. [globalObjectIdHash=951099334]. NetworkPrefab could not be found. Is the prefab registered with NetworkManager?
    2. UnityEngine.Debug:LogError (object)
    3. MLAPI.Logging.NetworkLog:LogError (string) (at Library/PackageCache/com.unity.multiplayer.mlapi@833f1faff4/Runtime/Logging/NetworkLog.cs:23)
    4. MLAPI.Spawning.NetworkSpawnManager:CreateLocalNetworkObject (bool,uint,ulong,System.Nullable`1<ulong>,System.Nullable`1<UnityEngine.Vector3>,System.Nullable`1<UnityEngine.Quaternion>,bool) (at Library/PackageCache/com.unity.multiplayer.mlapi@833f1faff4/Runtime/Spawning/NetworkSpawnManager.cs:221)
    5. MLAPI.NetworkObject:DeserializeSceneObject (MLAPI.Serialization.NetworkBuffer,MLAPI.Serialization.NetworkReader,MLAPI.NetworkManager) (at Library/PackageCache/com.unity.multiplayer.mlapi@833f1faff4/Runtime/Core/NetworkObject.cs:1016)
    6. MLAPI.Messaging.InternalMessageHandler:<HandleConnectionApproved>g__DelayedSpawnAction|5_0 (System.IO.Stream) (at Library/PackageCache/com.unity.multiplayer.mlapi@833f1faff4/Runtime/Messaging/InternalMessageHandler.cs:104)
    7. MLAPI.Messaging.InternalMessageHandler:HandleConnectionApproved (ulong,System.IO.Stream,single) (at Library/PackageCache/com.unity.multiplayer.mlapi@833f1faff4/Runtime/Messaging/InternalMessageHandler.cs:133)
    8. MLAPI.Profiling.InternalMessageHandlerProfilingDecorator:HandleConnectionApproved (ulong,System.IO.Stream,single) (at Library/PackageCache/com.unity.multiplayer.mlapi@833f1faff4/Runtime/Profiling/InternalMessageHandlerProfilingDecorator.cs:51)
    9. MLAPI.NetworkManager:HandleIncomingData (ulong,MLAPI.Transports.NetworkChannel,System.ArraySegment`1<byte>,single,bool) (at Library/PackageCache/com.unity.multiplayer.mlapi@833f1faff4/Runtime/Core/NetworkManager.cs:1227)
    10. MLAPI.NetworkManager:HandleRawTransportPoll (MLAPI.Transports.NetworkEvent,ulong,MLAPI.Transports.NetworkChannel,System.ArraySegment`1<byte>,single) (at Library/PackageCache/com.unity.multiplayer.mlapi@833f1faff4/Runtime/Core/NetworkManager.cs:1113)
    11. MLAPI.Transports.NetworkTransport:InvokeOnTransportEvent (MLAPI.Transports.NetworkEvent,ulong,MLAPI.Transports.NetworkChannel,System.ArraySegment`1<byte>,single) (at Library/PackageCache/com.unity.multiplayer.mlapi@833f1faff4/Runtime/Transports/NetworkTransport.cs:131)
    12. MLAPI.Transports.LiteNetLib.LiteNetLibTransport:LiteNetLib.INetEventListener.OnNetworkReceive (LiteNetLib.NetPeer,LiteNetLib.NetPacketReader,LiteNetLib.DeliveryMethod) (at Library/PackageCache/com.mlapi.contrib.transport.litenetlib@03f39bf2d3/Runtime/LiteNetLibTransport.cs:339)
    13. LiteNetLib.NetManager:ProcessEvent (LiteNetLib.NetEvent) (at Library/PackageCache/com.mlapi.contrib.transport.litenetlib@03f39bf2d3/Runtime/LiteNetLib/NetManager.cs:609)
    14. LiteNetLib.NetManager:PollEvents () (at Library/PackageCache/com.mlapi.contrib.transport.litenetlib@03f39bf2d3/Runtime/LiteNetLib/NetManager.cs:1361)
    15. MLAPI.Transports.LiteNetLib.LiteNetLibTransport:Update () (at Library/PackageCache/com.mlapi.contrib.transport.litenetlib@03f39bf2d3/Runtime/LiteNetLibTransport.cs:79)
    3. Last attempt is directly referencing these network object in inspector. These method works, but must manually create hard reference to the asset.

    Is this the intended design? it will be nice if I can get the reference from NetworkConfig directly or can use addressable to load it instead of reference directly in inspector. Thank you