A base building does not get connectionToClient when starting game?


I dont think my issue has to do with this episode, but I am not sure what caused it. It started some episodes before I think: When I start the game(MapScene) I get an Error saying:

Exception in OnStartServer:Object reference not set to an instance of an object at RTSPlayer.ServerHandleBuildingSpawned (Building building) [0x00064] in C:\Users\frede\Documents\GameDev\TankOFWarz\Assets\Scripts\Networking\RTSPlayer.cs:175
at (wrapper delegate-invoke) System.Action`1[Building].invoke_void_T(Building)

The Error accures in this code (I put some Debug.Logs in there):

private void ServerHandleBuildingSpawned(Building building)
Debug.Log("Trying to build following building: " + building);
if (connectionToClient== null)
Debug.Log(“connection to client is null”);
if(base.connectionToClient == null)
Debug.Log(“connection to base is null”);
Debug.Log("connectionID: " + base.connectionToClient.connectionId);
if (building.connectionToClient.connectionId != connectionToClient.connectionId)

I marked the critical Line as bold. It seems that the Value for “building.connectionToClient” is null. After using the Debug logs you can see above I realized, that this method gets called three times. The first two times it seems to work (“building.connectionToClient” and “connectionToClient” is not null) but the third time it causes the error. Do you have an Idea where my problem could be? I checked the code and compared it with the code in the git repository and could not find the cause for this issue.

Sry for the long text and thx in advance if you have an Idea what could help :slight_smile:

Kind regards

Hi there, what version of Mirror and Unity are you using?

thx for the reply. I am using Unity 2020.3.3f1 and Mirror 26.2.2.

kind regards

Ok I realized, that this mirror version is quite outdated and updated to 35.1.0. But the Problem stays. I also think that it might be a Problem in my code as I was able to start the game and Place Buildings in the game a few lectures ago.

Hi, when you update Mirror, delete the Mirror folder and do a fresh import. Let me know if that helps.

I tried the fresh import. Didn’t help :confused:

You said the method was being called three times. Are all three times by the same client/server?

yea I asked that myself as well. Not sure tbh. I dont understand Mirror enough to tell, but I thought maybe it gets called two times by the two clients and one time by server? maybe it gets called even more often. All I now is that the debug.log messages (see the code on the top) get called multiple times before the error accured. I will send a Screenshot.

Hi, so ServerOnBuildingSpawned, shouldn’t be called three times, it should only be called once by the server. Can you show your subscription statements for the ServerOnBuildingSpawned and also double check how these are being invoked by the Building.cs.

As for the error you’re getting:

What’s strange is that the first two times, it’s null. But the third time the reference is gone completely. Null means it’s looking at an empty container. That error implies there is no container to look at. In this case, I think it’s actually that “building” is null, so it can’t look at building.connectionToClient, a container that doesn’t exist for a null object.

Try changing your debug statements to look at the building. My hunch is that a client is calling this method, (which it shouldn’t be) before the building is synced to that client, so it doesn’t recognize the building.

Ok thx very much for the info. Now I am a step closer to understanding the problem :slight_smile:
The event is triggered in Building.cs in OnStartServer:

public override void OnStartServer()

the RTSPlayer.cs subscribes to it:

public override void OnStartServer()
Unit.ServerOnUnitSpawned += ServerHandleUnitSpawned;
Unit.ServerOnUnitDespawned += ServerHandleUnitDespawned;
Building.ServerOnBuildingSpawned += ServerHandleBuildingSpawned;
Building.ServerOnBuildingDespawned += ServerHandleBuildingDespawned;

public override void OnStopServer()
    Unit.ServerOnUnitSpawned -= ServerHandleUnitSpawned;
    Unit.ServerOnUnitDespawned -= ServerHandleUnitDespawned;
    Building.ServerOnBuildingSpawned -= ServerHandleBuildingSpawned;
    Building.ServerOnBuildingDespawned -= ServerHandleBuildingDespawned;

In my IDE (rider) I can see who is using the ServerOnBuildingSpawned event and it is only those two:

Ok I also just checked with debug.log messages and building is never null when ServerHandleBuildingSpawned gets called.

Okay, let’s back up here, the error is occurring when you start the game? What building is being spawned when the game starts? It’s the building script on your UnitBase right? Is the most recent version of the UnitBase prefab in the list of spawnable prefabs on the NetworkManager? Also it should be in the serialized field for “Unit Base Prefab”.

Ok I dragged the newest prefab version in the networkManager just to be sure, bu the problem remains. I am alwas testing with two Clients (one is the Game in the Unity Editor and one is a build of the game). As far as I understand the Game spawns two unit bases without problem (I can also see them in the scene shortly before the game crashes). But then the game wants to spawn a third building/base and can’t find a third Player (as there is none) and throws the error. Do I get that right? I also just checked the “players.Count” but it has the Value of two so that is not the problem.

thx for your full time help btw. :slight_smile: really appreciate it

You will only see the debug.log’s of the code being run in the server/client that’s in the editor. So assume whatever is happening is in that version. Just to check because I made this mistake once, when you press play in the editor, are you on the Scene_Menu?

no I start in the MenuScene.

oh big NEWS!
I tried hosting the other way around. Now the client was the host and the Game in the Unity editor joined the game. This time everything worked. No errors and I could place buildings and move the camera normaly. Is this Problem solved now? (I think so right?) But I dont understand what is the problem with the editor hosting the game and what caused the problem with the method being called to often.

Well the error could still be happening. It’s just happening in the build where you can’t see it. Does it affect the game when it happens in the editor?

if it happens in the editor the game stops.

Well that makes absolutely no sense lol.

Privacy & Terms