Getting NullReferenceException

Hi there,

I’m getting a NullReferenceException when calling in the Unit.cs script when I try to invoke the Action on “this” object.

Here’s the error output.

NullReferenceException: Object reference not set to an instance of an object
RTS.Networking.RTSPlayer.ServerHandleUnitSpawned (RTS.Units.Unit unit) (at Assets/Scripts/Networking/RTSPlayer.cs:29)
RTS.Units.Unit.OnStartServer () (at Assets/Scripts/Units/Unit.cs:32)
Mirror.NetworkIdentity.OnStartServer () (at Assets/Mirror/Runtime/NetworkIdentity.cs:673)
UnityEngine.Debug:LogException(Exception, Object)
Mirror.NetworkIdentity:OnStartServer() (at Assets/Mirror/Runtime/NetworkIdentity.cs:677)
Mirror.NetworkServer:SpawnObject(GameObject, NetworkConnection) (at Assets/Mirror/Runtime/NetworkServer.cs:1090)
Mirror.NetworkServer:Spawn(GameObject, NetworkConnection) (at Assets/Mirror/Runtime/NetworkServer.cs:1116)
RTS.Buildings.UnitSpawner:UserCode_CmdSpawUnits() (at Assets/Scripts/Buildings/UnitSpawner.cs:20)
RTS.Buildings.UnitSpawner:InvokeUserCode_CmdSpawUnits(NetworkBehaviour, NetworkReader, NetworkConnectionToClient)
Mirror.RemoteCalls.RemoteProcedureCalls:Invoke(Int32, RemoteCallType, NetworkReader, NetworkBehaviour, NetworkConnectionToClient) (at Assets/Mirror/Runtime/RemoteCalls.cs:120)
Mirror.NetworkIdentity:HandleRemoteCall(Byte, Int32, RemoteCallType, NetworkReader, NetworkConnectionToClient) (at Assets/Mirror/Runtime/NetworkIdentity.cs:1091)
Mirror.NetworkServer:OnCommandMessage(NetworkConnectionToClient, CommandMessage, Int32) (at Assets/Mirror/Runtime/NetworkServer.cs:979)
Mirror.<>c__DisplayClass6_0`2:<WrapHandler>b__0(NetworkConnection, NetworkReader, Int32) (at Assets/Mirror/Runtime/MessagePacking.cs:126)
Mirror.NetworkServer:UnpackAndInvoke(NetworkConnectionToClient, NetworkReader, Int32) (at Assets/Mirror/Runtime/NetworkServer.cs:470)
Mirror.NetworkServer:OnTransportData(Int32, ArraySegment`1, Int32) (at Assets/Mirror/Runtime/NetworkServer.cs:533)
Mirror.LocalConnectionToServer:Send(ArraySegment`1, Int32) (at Assets/Mirror/Runtime/LocalConnectionToServer.cs:45)
Mirror.NetworkConnection:Send(CommandMessage, Int32) (at Assets/Mirror/Runtime/NetworkConnection.cs:139)
Mirror.NetworkBehaviour:SendCommandInternal(String, NetworkWriter, Int32, Boolean) (at Assets/Mirror/Runtime/NetworkBehaviour.cs:248)
RTS.Buildings.UnitSpawner:CmdSpawUnits()
RTS.Buildings.UnitSpawner:OnPointerClick(PointerEventData) (at Assets/Scripts/Buildings/UnitSpawner.cs:32)
UnityEngine.EventSystems.EventSystem:Update() (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:501)

Hey there,

The null reference is probably occurring in the RTSPlayer in the ServerHandleUnitSpawned method.

Can you debug log the various parts and see which is null?

It would be weird if the unit was null, since you are passing it in directly.

Maybe the unit’s connectionToClient is null or the connectionId? This can happen if the connection isn’t created yet.

Perhaps if you share your RTSPlayer script that will help.

1 Like

Hey Yitzchak, thanks for the quick responde. Yeah, you’re right. The connectionToClient.connectionId is the null one on ServerHandleUnitSpawned method:

        private void ServerHandleUnitSpawned(Unit unit)
        {
            if (unit.connectionToClient.connectionId != connectionToClient.connectionId) {return;}
            myUnits.Add(unit);
        }

It should not be null since I’m getting the error once I’ve already started the Host (Server & Client), am I right? I’ve uploaded the projet to github so you can take a look.

Maybe it has to do with how your are spawning the unit. Does your unit have a network identity component attached? Is it in the list of spawnable prefabs on the Network Manager?

Also was it the unit’s connection that was null or the players? Just want to confirm that you checked both of those.

If those are all right, I’ll download your project and take a look. The code looks fine on the the player and the unit. Let me know if that helps.

Does your unit have a network identity component attached? Yes, it does:

Is it in the list of spawnable prefabs on the Network Manager? Yes, it is;

2

Also was it the unit’s connection that was null or the players?
The Unit’s connection was not null (it has the value 0 assigned if I’m not mistaken). The players connectionToClient was the object with the null reference.

Thanks agian Yitzchak, really appreciate it.

Just in case you’re wondering if the Player component has a NetworkIdentity attached to it, yes it does have one:

So the player has a a null connectionToClient??? Frankly, that makes no sense lol. What version of Mirror are you using?

jajaja totally agree. I must be missing something really obvious because it makes no sense.
Mirror version:
Version 66.0.9 - April 17, 2022

I run the debug again with the breakpoint inside of the ServerHandleUnitSpawned method and took a few screenshoots:



6

I guess let’s go back a another step and make sure the RTSNetwork Manager is assigning it a connection?

public class RTSNetworkManager : NetworkManager
    {
        [SerializeField] private GameObject unitSpawnerPrefab = null;
        public override void OnServerAddPlayer(NetworkConnectionToClient conn)
        {
            base.OnServerAddPlayer(conn);
            
            GameObject unitSpawnerInstance = Instantiate(
                unitSpawnerPrefab, 
                conn.identity.transform.position, 
                conn.identity.transform.rotation);
            NetworkServer.Spawn(unitSpawnerInstance, conn);
        }
    }

See if the conn being used in Spawn is null or not.

The conn in the NetworkServer.Spawn is assigning the player a valid identity:

2

I’m super confused I set the breakpoint here on the UnitSpawner.cs and the connectionToClient is not null and it’s pointing to the “Player” identity:

        [Command]
        private void CmdSpawUnits()
        {
            GameObject unitInstance = Instantiate(unitPrefab, unitSpawnPoint.position, unitSpawnPoint.rotation);
            NetworkServer.Spawn(unitInstance, connectionToClient);
        }

So the connection identity is not null on the player and not null on the unity. But when you try and compare them in onunitspawn, the player connection is null?

I would try a different version of Mirror.

Also debug in the network manager when it is spawning the player, maybe there is an issue there?

Hi Yitzchak, hope you’re doing fine.

But when you try and compare them in onunitspawn, the player connection is null?” That’s right.

I debugged the network manager but I don’t see any issues with player, it has a valid network identity.

I’ve tried the latest Mirror version 67.1.0 and I’m still having this same issue. I was working with 66.0.9 version before. Do you recommend a specific version? I see that Nathan used 16.9.0 but it was a few years ago so…
BTW I’m using Unity 2021.3.4f1

Well I downloaded your project, and figured out the problem. Try deleting the player prefab that is in the scene already. It gets spawned by the network manager, so you don’t need the one in the scene before you start the game. There must have been a conflict between the one already there, and the one spawned by the network manager.

Delete this guy:
image

2 Likes

Yitzchack, thanks you so much. I knew it was something super dumb.

You are most welcome! It’s never super dumb. Sometimes it’s easy to get tunnel vision on a bug and miss the seemingly unconnected cause. A second set of eyes always goes a long way.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms