I am getting a NullReferenceException when ever I try to select multiple units

Below is the error that I get every time i try and drag select multiple units;

The error is in relation to the player here;

I’ve reviewed the git repo and my code appears to be the same. Am I just missing something painfully obvious?
I’m new to unity dev so I could very well be.

1 Like

Hi there, there is a common error in copy and pasting in the subscription methods for adding units to the unitList in the RTSPlayer.cs. Please check that your subscriptions are done correctly and that all “+” or “-” are correct and that it says Despawn or Spawn where it should. A list with no values could create this error.

public override void OnStartServer()
    {
        Unit.ServerOnUnitSpawned += ServerHandleUnitSpawned;
        Unit.ServerOnUnitDespawned += ServerHandleUnitDespawned;
public override void OnStopServer()
    {
        Unit.ServerOnUnitSpawned -= ServerHandleUnitSpawned;
        Unit.ServerOnUnitDespawned -= ServerHandleUnitDespawned;

Mine looks like yours and still the same issue;

public override void OnStartServer()
{
Unit.ServerOnUnitSpawned += ServerHandleUnitSpawned;
Unit.ServerOnUnitDespawned += ServerHandleUnitDespawned;
}

public override void OnStopServer()
{
    Unit.ServerOnUnitSpawned -= ServerHandleUnitSpawned;
    Unit.ServerOnUnitDespawned -= ServerHandleUnitDespawned;
}

My click still works correctly. So I select and deselect units with a simple left click but the drag box for multiple units does not work and causes the null reference to the thrown in the console.

Is there a way to hit break points in unity if i place them in my code? Trying to see if the player object is null.

Yes you can use a debugger to create break points are you using VS Code? You will need the correct extension for your code editor.

Maybe the error is in the GetMyUnits() method, can you share your code for that method?

public List GetMyUnits()
{
return myUnits;
}

Just this method?

Whoops, guess that wasn’t helpful. Can you drop some Debug.Log statements into the script so can confirm what is triggering the null reference.

Before the foreach loop is run put in:

Debug.Log(player);
Debug.Log(player.GetMyUnits());

The error is on that line right?

So the player is Null at this point it seems.

Just o be sure, I inserted the above debug as shown below;

Debug.Log(player);
Debug.Log(player.GetMyUnits());

    foreach (Unit unit in player.GetMyUnits())
    {
        Vector3 screenPosition = mainCamera.WorldToScreenPoint(unit.transform.position);

        if (screenPosition.x > min.x &&
            screenPosition.x < max.x &&
            screenPosition.y > min.y &&
            screenPosition.y < max.y)
        {
            SelectedUnits.Add(unit);
            unit.Select();
        }
    }

The player is null? Are you getting a hold of the player in the Start() function?

private void Start() 

    {
        mainCamera = Camera.main;
        player = NetworkClient.connection.identity.GetComponent<RTSPlayer>();

No but I was in the update function. I tried placing it in the start function but the same issue persists.

So after your suggestion it looks like this;

private void Start()
{
mainCamera = Camera.main;
player = NetworkClient.connection.identity.GetComponent();
}

private void Update()
{
    if (player == null)
    {
        player = NetworkClient.connection.identity.GetComponent<RTSPlayer>();
    }

But I am still seeing the issue.

Well, getting the player in update is more robust in this case, so better to stick with that for now. If you put a debug.log() in update after the if statement, do you see it getting a hold of the player eventually?

private void Update()
{
    if (player == null)
    {
        player = NetworkClient.connection.identity.GetComponent<RTSPlayer>();
    }
    Debug.Log(player);

So the player never seems to be created. It is permanently null.

image

Okay, I would venture a guess that this is related to how your Player prefab is set up. Make sure it has a NetworkIdentity and RTSPlayer all on the same gameObject. On the networkManager side, make sure the correct player prefab is in the player prefab field and add the player prefab to the registered spawnable prefabs as well just in case.

Also try loading the game and taking a look at the player objects in the hierarchy and see how they are set up, make sure everything looks correct.

plz help me
whan i play game
than error i sow
exception in MessageHandler: UnassignedReferenceException The variable displayColorRenderer of MyNetworkPlayer has not been assigned.

here the code
using System.Collections;

using System.Collections.Generic;

using Mirror;

using UnityEngine;

using TMPro;

public class MyNetworkPlayer : NetworkBehaviour

{

[SerializeField] private TMP_Text displayNameText = null;

[SerializeField] private Renderer displayColorRenderer = null;

[SyncVar]

[SerializeField]

private string displayName = “Missing Name”;

[SyncVar(hook = nameof(HandleDisplayColorUpdated))]

[SerializeField]

private Color displayColor = Color.black;

[Server]

public void SetDisplayName(string NewDisplayName)

{ displayName = NewDisplayName;}

 [Server]

public void SetDisplayColor(Color NewDisplayColor)

{ displayColor = NewDisplayColor;}

private void HandleDisplayColorUpdated (Color oldColor, Color newColor)

{

    displayColorRenderer.material.SetColor("_BaseColor", newColor);

}

}

I fix it
plz don’t replay
for solve my problem

That was he issue! My prefab never had RTSPlayer script attached to it.
I must have missed that in the tutorial in earlier steps. Thanks a lot for the help! I really appreciate it!

Hi there, glad you figured it out, best to share your solution for others to see.

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

Privacy & Terms