OnClientConnectedCallback not being called

Hi all,

I have this StartServer function…

public void StartServer()
    {
        Debug.Log("Server Started");
        NetworkManager.Singleton.OnClientConnectedCallback += NetworkManager_OnClientConnectedCallback;
        NetworkManager.Singleton.StartServer();
    }

private void NetworkManager_OnClientConnectedCallback(ulong clientId)
    {
        Debug.Log("ClientId: " + clientId);
        playerDataNetworkList.Add(new PlayerData
        {
            clientId = clientId
        });
    }

public void StartClient()
    {
        NetworkManager.Singleton.OnClientConnectedCallback += NetworkManager_Client_OnClientConnectedCallback;
        NetworkManager.Singleton.StartClient();
    }

And then 2 buttons for a server and player to test with

private void Awake()
    {
        createGame.onClick.AddListener(() =>
        {
            MobaGameMultiplayer.Instance.StartServer();
            NetworkManager.Singleton.SceneManager.LoadScene("CharacterSelect", LoadSceneMode.Single);
        });

        joinGame.onClick.AddListener(() =>
        {
            MobaGameMultiplayer.Instance.StartClient();

        });
    }

I connect using ParrelSync and have 3 editors open. The first one creates a server and says “Server Started” correctly. Then the next 2 players to join game just get the error:

Here is the full error. Any reaon why the Debug Log ClientId: is not running at all on player connection?

KeyNotFoundException: The given key '-2150' was not present in the dictionary.
System.Collections.Generic.Dictionary`2[TKey,TValue].get_Item (TKey key) (at <dc753a1061284f8e971ee88ee4826eee>:0)
Unity.Netcode.NetworkSceneManager.GetSceneRelativeInSceneNetworkObject (System.UInt32 globalObjectIdHash, System.Nullable`1[T] networkSceneHandle) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.8.0/Runtime/SceneManagement/NetworkSceneManager.cs:981)
Unity.Netcode.NetworkSpawnManager.CreateLocalNetworkObject (Unity.Netcode.NetworkObject+SceneObject sceneObject) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.8.0/Runtime/Spawning/NetworkSpawnManager.cs:511)
Unity.Netcode.NetworkObject.AddSceneObject (Unity.Netcode.NetworkObject+SceneObject& sceneObject, Unity.Netcode.FastBufferReader reader, Unity.Netcode.NetworkManager networkManager) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.8.0/Runtime/Core/NetworkObject.cs:1842)
Unity.Netcode.SceneEventData.SynchronizeSceneNetworkObjects (Unity.Netcode.NetworkManager networkManager) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.8.0/Runtime/SceneManagement/SceneEventData.cs:933)
Unity.Netcode.NetworkSceneManager.HandleClientSceneEvent (System.UInt32 sceneEventId) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.8.0/Runtime/SceneManagement/NetworkSceneManager.cs:2106)
Unity.Netcode.NetworkSceneManager.ClientLoadedSynchronization (System.UInt32 sceneEventId) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.8.0/Runtime/SceneManagement/NetworkSceneManager.cs:2002)
Unity.Netcode.SceneEventProgress.<SetAsyncOperation>b__37_0 (UnityEngine.AsyncOperation asyncOp2) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.8.0/Runtime/SceneManagement/SceneEventProgress.cs:270)
UnityEngine.AsyncOperation.InvokeCompletionEvent () (at <6f7018b8b8c149e68c4a65a05ac289be>:0)

I will make a guess here: perhaps because you assign the callback BEFORE you actually call StartServer/StartClient?

I’ve always assigned event callbacks after Start… and never had this issue.

That should be fine, at least I assign them before, there is a custom message callback which I recall needs to be assigned after the Start call.

The error looks related to an in-scene object but I can’t tell any more than that…

Btw starting with NGO 1.8 you’re supposed to use the multi-purpose ConnectionEvent callback. Try that if you’re on 1.8+

I think it cannot find key -2150 in this Dictionary

/// Since Scene.handle is unique per client, we create a look-up table between the client and server to associate server unique scene
        /// instances with client unique scene instances
        /// </summary>
        internal Dictionary<int, int> ServerSceneHandleToClientSceneHandle = new Dictionary<int, int>();

Which is inside NetworkSceneManager.

Does it revolve around this code?

            NetworkManager.Singleton.SceneManager.LoadScene("CharacterSelect", LoadSceneMode.Single);

And here is a related warning that I forgot to mention that happens before the error

[SceneEventData- Scene Handle Mismatch] serverSceneHandle could not be found in ServerSceneHandleToClientSceneHandle. Using the currently active scene.
UnityEngine.Debug:LogWarning (object)
Unity.Netcode.NetworkSceneManager:SetTheSceneBeingSynchronized (int) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.8.0/Runtime/SceneManagement/NetworkSceneManager.cs:964)
Unity.Netcode.SceneEventData:SynchronizeSceneNetworkObjects (Unity.Netcode.NetworkManager) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.8.0/Runtime/SceneManagement/SceneEventData.cs:931)
Unity.Netcode.NetworkSceneManager:HandleClientSceneEvent (uint) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.8.0/Runtime/SceneManagement/NetworkSceneManager.cs:2106)
Unity.Netcode.NetworkSceneManager:ClientLoadedSynchronization (uint) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.8.0/Runtime/SceneManagement/NetworkSceneManager.cs:2002)
Unity.Netcode.SceneEventProgress:<SetAsyncOperation>b__37_0 (UnityEngine.AsyncOperation) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.8.0/Runtime/SceneManagement/SceneEventProgress.cs:270)
UnityEngine.AsyncOperation:InvokeCompletionEvent ()

If server/host and client scenes are not identical this could happen. ParrelSynch projects sometimes go out of synch, delete and recreate the clone may help.

Ah man you’re right. I won’t get errors with a build and the editor. Super lame. I am not very far in my game to be building every 5 seconds to get a working test going.
Any advice to move forward from here?

Absolutely!

Multiplayer Playmode :slight_smile:

It’s absolutely worth upgrading to Unity 6 just for this. See it in action, it works just like enter playmode except it can connect up to four players:

https://www.youtube.com/watch?v=tXFRm7IhBAA

Ah damn. Unity 6 is very cool and I love the Multiplay so I will keep trying to use it but unfortunately it has the same error I am facing. I might play around with my code and see if the problem exists there as I won’t be able to advance until I fix this.

Thanks

Omg after an ass load of debugging I found the solution. A GameObject in my Lobby scene is set as an Instance with DontDestroyOnLoad. That was a Network object so when I tried to remove the NetworkObject component for it, it re-added it with a new GlobalObjectIdHash. This simply fixed it. Something must have fallen out of sync at some point.