Double-spawned NetworkManager Player objects when using NetworkSceneManager

Unity 6.1.4, NGO 2.3.2
Using Client>Server Topology

When a client connects to the server I’m seeing two copies of the Player object get created on the client side only.

From looking at logs it seems this may be a bug with NetworkSceneManager.

For context I’m testing this on a local machine, doesn’t matter if it’s in builds or running Play Mode Scenarios, guessing it might be NetworkSceneManager jumping the gun on (re?)syncing because the initial scene load/sync happens near instantaneously vs. network latency giving it an opportunity to realize the 2nd sync isn’t needed.

For now I’ll be hacking around this by checking on state of Player object post instantiation and Destroying it if it’s not networked.

Logs below:

1st Player object Awake():

IdleMMO.NetworkedEntities.Players.Player:Awake () (at C:/Users/ben/Documents/Tahoma Eternal/tahoma-eternal/Assets/Scripts/Game/Players/Player.cs:32)
UnityEngine.Object:Instantiate<UnityEngine.GameObject> (UnityEngine.GameObject)
Unity.Netcode.NetworkSpawnManager:InstantiateNetworkPrefab (UnityEngine.GameObject,uint,System.Nullable`1<UnityEngine.Vector3>,System.Nullable`1<UnityEngine.Quaternion>) (at ./Library/PackageCache/com.unity.netcode.gameobjects@91cf39ffcce4/Runtime/Spawning/NetworkSpawnManager.cs:863)
Unity.Netcode.NetworkSpawnManager:GetNetworkObjectToSpawn (uint,ulong,System.Nullable`1<UnityEngine.Vector3>,System.Nullable`1<UnityEngine.Quaternion>,bool) (at ./Library/PackageCache/com.unity.netcode.gameobjects@91cf39ffcce4/Runtime/Spawning/NetworkSpawnManager.cs:840)
Unity.Netcode.NetworkSpawnManager:CreateLocalNetworkObject (Unity.Netcode.NetworkObject/SceneObject) (at ./Library/PackageCache/com.unity.netcode.gameobjects@91cf39ffcce4/Runtime/Spawning/NetworkSpawnManager.cs:891)
Unity.Netcode.NetworkObject:AddSceneObject (Unity.Netcode.NetworkObject/SceneObject&,Unity.Netcode.FastBufferReader,Unity.Netcode.NetworkManager,bool) (at ./Library/PackageCache/com.unity.netcode.gameobjects@91cf39ffcce4/Runtime/Core/NetworkObject.cs:3217)
Unity.Netcode.SceneEventData:SynchronizeSceneNetworkObjects (Unity.Netcode.NetworkManager) (at ./Library/PackageCache/com.unity.netcode.gameobjects@91cf39ffcce4/Runtime/SceneManagement/SceneEventData.cs:1137)
Unity.Netcode.NetworkSceneManager:HandleClientSceneEvent (uint) (at ./Library/PackageCache/com.unity.netcode.gameobjects@91cf39ffcce4/Runtime/SceneManagement/NetworkSceneManager.cs:2388)
Unity.Netcode.NetworkSceneManager:ClientLoadedSynchronization (uint) (at ./Library/PackageCache/com.unity.netcode.gameobjects@91cf39ffcce4/Runtime/SceneManagement/NetworkSceneManager.cs:2283)
Unity.Netcode.SceneEventProgress:<SetAsyncOperation>b__37_0 (UnityEngine.AsyncOperation) (at ./Library/PackageCache/com.unity.netcode.gameobjects@91cf39ffcce4/Runtime/SceneManagement/SceneEventProgress.cs:272)
UnityEngine.AsyncOperation:InvokeCompletionEvent ()

2nd Player object Awake():

IdleMMO.NetworkedEntities.Players.Player:Awake () (at C:/Users/ben/Documents/Tahoma Eternal/tahoma-eternal/Assets/Scripts/Game/Players/Player.cs:32)
UnityEngine.Object:Instantiate<UnityEngine.GameObject> (UnityEngine.GameObject)
Unity.Netcode.NetworkSpawnManager:InstantiateNetworkPrefab (UnityEngine.GameObject,uint,System.Nullable`1<UnityEngine.Vector3>,System.Nullable`1<UnityEngine.Quaternion>) (at ./Library/PackageCache/com.unity.netcode.gameobjects@91cf39ffcce4/Runtime/Spawning/NetworkSpawnManager.cs:863)
Unity.Netcode.NetworkSpawnManager:GetNetworkObjectToSpawn (uint,ulong,System.Nullable`1<UnityEngine.Vector3>,System.Nullable`1<UnityEngine.Quaternion>,bool) (at ./Library/PackageCache/com.unity.netcode.gameobjects@91cf39ffcce4/Runtime/Spawning/NetworkSpawnManager.cs:840)
Unity.Netcode.NetworkSpawnManager:CreateLocalNetworkObject (Unity.Netcode.NetworkObject/SceneObject) (at ./Library/PackageCache/com.unity.netcode.gameobjects@91cf39ffcce4/Runtime/Spawning/NetworkSpawnManager.cs:891)
Unity.Netcode.NetworkObject:AddSceneObject (Unity.Netcode.NetworkObject/SceneObject&,Unity.Netcode.FastBufferReader,Unity.Netcode.NetworkManager,bool) (at ./Library/PackageCache/com.unity.netcode.gameobjects@91cf39ffcce4/Runtime/Core/NetworkObject.cs:3217)
Unity.Netcode.CreateObjectMessage:CreateObject (Unity.Netcode.NetworkManager&,ulong,uint,Unity.Netcode.NetworkObject/SceneObject,Unity.Netcode.FastBufferReader,ulong[],ulong[]) (at ./Library/PackageCache/com.unity.netcode.gameobjects@91cf39ffcce4/Runtime/Messaging/Messages/CreateObjectMessage.cs:215)
Unity.Netcode.CreateObjectMessage:CreateObject (Unity.Netcode.NetworkManager&,Unity.Netcode.NetworkSceneManager/DeferredObjectCreation&) (at ./Library/PackageCache/com.unity.netcode.gameobjects@91cf39ffcce4/Runtime/Messaging/Messages/CreateObjectMessage.cs:204)
Unity.Netcode.NetworkSceneManager:ProcessDeferredCreateObjectMessages () (at ./Library/PackageCache/com.unity.netcode.gameobjects@91cf39ffcce4/Runtime/SceneManagement/NetworkSceneManager.cs:3196)
Unity.Netcode.NetworkSceneManager:HandleClientSceneEvent (uint) (at ./Library/PackageCache/com.unity.netcode.gameobjects@91cf39ffcce4/Runtime/SceneManagement/NetworkSceneManager.cs:2394)
Unity.Netcode.NetworkSceneManager:ClientLoadedSynchronization (uint) (at ./Library/PackageCache/com.unity.netcode.gameobjects@91cf39ffcce4/Runtime/SceneManagement/NetworkSceneManager.cs:2283)
Unity.Netcode.SceneEventProgress:<SetAsyncOperation>b__37_0 (UnityEngine.AsyncOperation) (at ./Library/PackageCache/com.unity.netcode.gameobjects@91cf39ffcce4/Runtime/SceneManagement/SceneEventProgress.cs:272)
UnityEngine.AsyncOperation:InvokeCompletionEvent ()

This then triggers this warning :

[NetworkManager] Trying to spawn Player(Clone) with a NetworkObjectId of 6 but it is already in the spawned list!
UnityEngine.Debug:LogWarning (object)
Unity.Netcode.NetworkSpawnManager:SpawnNetworkObjectLocallyCommon (Unity.Netcode.NetworkObject,ulong,bool,bool,ulong,bool) (at ./Library/PackageCache/com.unity.netcode.gameobjects@91cf39ffcce4/Runtime/Spawning/NetworkSpawnManager.cs:1117)
Unity.Netcode.NetworkSpawnManager:SpawnNetworkObjectLocally (Unity.Netcode.NetworkObject,Unity.Netcode.NetworkObject/SceneObject&,bool) (at ./Library/PackageCache/com.unity.netcode.gameobjects@91cf39ffcce4/Runtime/Spawning/NetworkSpawnManager.cs:1107)
Unity.Netcode.NetworkObject:AddSceneObject (Unity.Netcode.NetworkObject/SceneObject&,Unity.Netcode.FastBufferReader,Unity.Netcode.NetworkManager,bool) (at ./Library/PackageCache/com.unity.netcode.gameobjects@91cf39ffcce4/Runtime/Core/NetworkObject.cs:3263)
Unity.Netcode.CreateObjectMessage:CreateObject (Unity.Netcode.NetworkManager&,ulong,uint,Unity.Netcode.NetworkObject/SceneObject,Unity.Netcode.FastBufferReader,ulong[],ulong[]) (at ./Library/PackageCache/com.unity.netcode.gameobjects@91cf39ffcce4/Runtime/Messaging/Messages/CreateObjectMessage.cs:215)
Unity.Netcode.CreateObjectMessage:CreateObject (Unity.Netcode.NetworkManager&,Unity.Netcode.NetworkSceneManager/DeferredObjectCreation&) (at ./Library/PackageCache/com.unity.netcode.gameobjects@91cf39ffcce4/Runtime/Messaging/Messages/CreateObjectMessage.cs:204)
Unity.Netcode.NetworkSceneManager:ProcessDeferredCreateObjectMessages () (at ./Library/PackageCache/com.unity.netcode.gameobjects@91cf39ffcce4/Runtime/SceneManagement/NetworkSceneManager.cs:3196)
Unity.Netcode.NetworkSceneManager:HandleClientSceneEvent (uint) (at ./Library/PackageCache/com.unity.netcode.gameobjects@91cf39ffcce4/Runtime/SceneManagement/NetworkSceneManager.cs:2394)
Unity.Netcode.NetworkSceneManager:ClientLoadedSynchronization (uint) (at ./Library/PackageCache/com.unity.netcode.gameobjects@91cf39ffcce4/Runtime/SceneManagement/NetworkSceneManager.cs:2283)
Unity.Netcode.SceneEventProgress:<SetAsyncOperation>b__37_0 (UnityEngine.AsyncOperation) (at ./Library/PackageCache/com.unity.netcode.gameobjects@91cf39ffcce4/Runtime/SceneManagement/SceneEventProgress.cs:272)
UnityEngine.AsyncOperation:InvokeCompletionEvent ()

While I’m not too familiar with the networking side of things myself, this does look like it might be an actual bug. I’d suggest submitting a bug from within the Editor by selecting “Help → Report a Bug…” and attaching your project so our Customer QA team can take a look. I’d also appreciate it if you could share the ticket ID (IN-XXXXX) after you’ve done so.

Could you provide us with some reference to how you are spawning players?
Are you using the normal NetworkManager player prefab assignment or are you manually spawning?
If the later, can you provide that script?

For reference, you can look at this example and see if you are doing something differently?

Things to check:

  • If the NetworkManager’s player prefab is set then NGO will automatically spawn the player.
    • If you have your own script spawning a player, then you will get two instances of the player.
  • Make sure you are not trying to make an in-scene placed NetworkObject the player object.

Other than this, I would need to know more about your project’s setup regarding player spawning.

Hi, Players are getting auto-spawned by NetworkManager using the assigned player prefab.

Bug reported … IN-103953

Hi @Aurigan,

Ok, I was assigned your bug report, opened your project, and was able to replicate your issue.

So, this seems to be related to a NetworkShow issue.

Just to make sure I am following your project’s setup (correct me if I am wrong)
You have a pretty complex setup where it looks like you have:

  • The Player network prefab auto-spawn for each connected client.
    • Looks like this is how players interact with things and their avatar?
    • You have the spawn with observers turned off.
      • This is made visible within the player script by the server.
  • The Avatar network prefab that is spawned by the server when a client begins to connect.
    • The avatar is the spawned object with visuals that represents the player but is controlled by the server (based on player inputs etc).

What is happening

NetworkShow commands are actually deferred until the end of the frame due to other issues where a NetworkObject was shown but changes to NetworkVariables were not included. This fix for the NeteworkVariable synchronization issue has caused some other issues. The most recent one resolved was in PR-3468.

I need to use the same approach when sending synchronization information to a newly joining client where it will do the same IsObjectVisibilityPending check when building the synchronization scene event and exclude anything that has pending visibility.

Temporary Work Around
For now, you might re-check the spawn with observers flag on the player prefab and comment out the line in the player.cs file where the server is making the player instance visible. It didn’t seem to impact anything when running and the duplicate objects issue went away.

The Fix
I will put together a PR to handle this issue. Thank you for providing your project as reference (I will use it for my manual test while fixing the issue) and I will post back here once the PR is posted.

Ima take this as a compliment :wink:

I think you have the setup understood correctly … for this project I want avatars to be persistent even if the player/client isn’t connected, the Player object is just there to communicate client<->server over what the avatar should be doing/has done.

For now, you might re-check the spawn with observers flag on the player prefab

I don’t need/want the Player network object to be synced to any client other than the owning one (because … preemptive optimization/concerns about security) which is how I ended up toggling off observers in the first place, possibly there’s some neater solution I’m not aware of here to achieve the same end(?).

Good to hear it’s a real bug/can be fixed, appreciate the speedy investigation. Cheers.

I just pushed the branch with the fix.
If you wanted, you can replace your manifest com.unity.netcode.gameobjects entry with this:

"com.unity.netcode.gameobjects": "https://github.com/Unity-Technologies/com.unity.netcode.gameobjects.git?path=com.unity.netcode.gameobjects#fix/networkshow-client-synchronization-duplicate-players",

This resolves the issue. It will take a couple of weeks before the patch for this is available and so I wanted to provide you with a way to continue developing your project without having to compromise on your project’s needs.

At a minimum, give the branch a try and let me know if your issue is resolved? (Seems to work fine using your project on my end using that fix).

Confirmed patch fixes issue … great!

PR-3488 is up and will be in the next schedule patch update.