unity-game-enginenetwork-programming

Unity MLAPI's NetworkObject.ChangeOwner() gives KeyNotFoundException


I am trying to get into Multiplayer games with MLAPI, and came across an issue I'm not able to resolve.

void Update()
    {
        if (Input.GetKeyDown(KeyCode.S))
        {
            GameObject player = Instantiate(cube);
            player.GetComponent<NetworkObject>().Spawn();
            player.GetComponent<NetworkObject>().ChangeOwnership(2);
        }
    }

I've put this script on an Empty GameObject, cube just being a cube Prefab with a NetworkObject Component attached to it. It's supposed to spawn the object and then change the ownership of it. But, when i start one instance of the project as server and the other as client, and press S on the server, i get this error on the Client:

KeyNotFoundException: The given key was not present in the dictionary.
System.Collections.Generic.Dictionary`2[TKey,TValue].get_Item (TKey key) (at <9577ac7a62ef43179789031239ba8798>:0)
MLAPI.Messaging.InternalMessageHandler.HandleChangeOwner (System.UInt64 clientId, System.IO.Stream stream) (at F:/1Unity/Networking Project/Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Messaging/InternalMessageHandler.cs:351)*

(It's actually longer, but I think the rest should be unnecessary, let me know if it's not.)

Putting any other value than 2 inside the ChangeOwnership Method and trying the same thing gives this on the Server:

KeyNotFoundException: The given key was not present in the dictionary.
System.Collections.Generic.Dictionary`2[TKey,TValue].get_Item (TKey key) (at <9577ac7a62ef43179789031239ba8798>:0)
MLAPI.Spawning.NetworkSpawnManager.ChangeOwnership (MLAPI.NetworkObject networkObject, System.UInt64 clientId) (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Spawning/NetworkSpawnManager.cs:231)*

I would appreciate if anyone could help me out with this.


Solution

  • Why not using SpawnWithOwnership? Oh I know, it would be too simple ;)