(Just tell me if you need more details for this problem and what details are those. Thanks!)
I am encountering the errors below in the Mirror network package (Unity).
I am new at networking in Unity and I can’t figure out what causes this error.
Disconnecting connId=0 to prevent exploits from an Exception in MessageHandler: NullReferenceException Object reference not set to an instance of an object
at Mirror.NetworkIdentity.OnStartServer () [0x00085] in C:\Users\...\My project\Assets\Mirror\Runtime\NetworkIdentity.cs:659
at Mirror.NetworkServer.SpawnObject (UnityEngine.GameObject obj, Mirror.NetworkConnection ownerConnection) [0x000af] in C:\Users\....\My project\Assets\Mirror\Runtime\NetworkServer.cs:1021
NullReferenceException: Object reference not set to an instance of an object
Mirror.NetworkIdentity.ClearAllComponentsDirtyBits () (at Assets/Mirror/Runtime/NetworkIdentity.cs:1234)
Mirror.NetworkServer.ClearSpawnedDirtyBits () (at Assets/Mirror/Runtime/NetworkServer.cs:1571)
Mirror.NetworkServer.Broadcast () (at Assets/Mirror/Runtime/NetworkServer.cs:1689)
Here is the situation:
I am trying to create a game that has a host and a server. I created my own IP input field to allow changing host IP in the game. But when I click “HOST” which calling StartServer() function of Mirror Network Manager, it produces the error below. It is also the case when trying the HUD.
Note that I haven’t edited or coded yet anything with Mirror in this project.
The code in the Player prefab is working when I run the game scene alone.
Above is the first scene where the user needs to input the host IP. The Play button is for the client connection, but don’t mind it yet.
I’m not sure exactly what’s going on but a little clarification. The player that is the host is the server as well as a client, with the other players joining the host as clients. You don’t need to set the IP for the host and I would keep it simple and leave as localhost for now, and start the server with a call to NetworkManager.singleton.StartHost(). The joining players can enter the IP as localhost, and join the server with NetworkManager.singleton.StartClient().
I don’t know why the hud would also give problems, check out the examples that come with Mirror and see if they’re working.
I had the same problem and fixed it. The problem was that apparently Mirror can not take Gameobject as an input to a command. I just received the gameobject from the command function input parameters and the problem was resolved.
Unfortunately the only correct answer is, the server encountered an error when parsing data received from the client.
You will have to dig into the core and debug to see what was the last packet received from the client leading up to the error, and further isolate if it’s a RPC, broadcast, ect.
Once you know the origin see if it’s the data being received or possibly the logic processing the data that’s throwing.
Clients should never be able to throw exceptions on the server.
Otherwise, you are risking potential exploits.
For example, let’s say you have two components:
PlayerCombat
PlayerBuffs
and let’s say PlayerBuffers.OnStartServer loads all buffs from the database.
Buffs may be positive, or negative - for example, some kind of punishment that lasts for a day.
Now if an attacker is able to throw an exception in PlayerCombat.OnStartServer, then PlayerBuffs.OnStartServer may never happen due to the exception being propagated up the call stack.
In other words, in that hypothetical example an attacker may be able to remove (= never load) negative buffs by triggering the exception.
This is why Mirror disconnects a client if that client was able to throw an exception.
The goal is to contain the potential attack.
Now in your example, it looks like NetworkIdentity:1234 throws a NullReferenceException.
This should never happen, it sounds like some component in your game is null or was destroyed when it shouldn’t be.
For example, don’t call GameObject.Destroy(playerCombat) manually.
Always use NetworkServer.Destroy(player.gameObject).
Mirror will always show you the reason why it disconnect a connection.
Follow the reason and you will know the answer