Recently hopped on Mirror Networking and having some troubles with commands
So, I have some blocks that are instantiated at runtime and they can be clicked.
My logic is that when block is clicked, then i send a message to a server with id of the block, then server through a game manager, which has list of all blocks, perform a click on the block with that id for each client, using ClientRpc.
This is working just fine on the host instance, but not on the client one.
The code is simple
[Command(requiresAuthority = false)]
public void SendClickToAServer(int index)
{
Debug.Log("Clicked index:" + index);
}
But I'm having an error and client gets disconnected
I need to send a message to a server from client, so I'm using command. When I try to do it on host instance, then every thing is fine, but when i try to send this message from just client then i'm getting an error Disconnecting connection: connection(390227533) because handling a message of type Mirror.CommandMessage caused an Exception. This can happen if the other side accidentally (or an attacker intentionally) sent invalid data. Reason: System.NullReferenceException: Object reference not set to an instance of an object at Mirror.NetworkServer.OnCommandMessage (Mirror.NetworkConnectionToClient conn, Mirror.CommandMessage msg, System.Int32 channelId) [0x00103] in \Mirror\Core\NetworkServer.cs:327
I add all needed components and inheritance, as I said on the host instance it is working, i just can not get what object reference is missing. Maybe there is problem with the way i instantiate or spawn objects?
If i remove [Command] then every thing works fine, but it is hot synced
Hmm.. It's hard for me to tell what's wrong, but there's nothing wrong with the code you provided (nor with your workflow).
If it is running fine on the host and not the client then you have an issue where your client is handling an incorrect NetworkIdentity
(most likely) and the state of the game is already out of sync before you send the Command
. Check to make sure that the objects you are spawning are spawned on the server and synced to the client (rather than spawning a NetworkIdentity
directly on the client). Make sure you aren't spawning the Player Prefab
twice as well as they get created by the NetworkManager
by default when connecting to the server (as in, make sure they're not already in your scene before launching the server).