Still Getting an Error

Even after this video, I am still receiving a NullReferenceException after the unit dies. I believe I have followed everything correctly but my console doesn’t tell me where the error is.

Unit Class

public class Unit : MonoBehaviour
{  
    private Move move;
    private Spin spin;
    private BaseAction[] actionArray;
    private HealthSystem health;
   
    private GridPosition currentPosition;
    [SerializeField] private int BaseActionPoints;
    [SerializeField] private bool isEnemy;
    private int actionPoints = 2;
    public static event EventHandler OnAnyActionPointsChange;
    private void Awake(){
        move = GetComponent<Move>();
        spin = GetComponent<Spin>();
        actionArray = GetComponents<BaseAction>();
        health = GetComponent<HealthSystem>();
        health.onDeath += Unit_onDeath;
    }
    private void Start(){
        TurnSystem.Instance.OnTurnChange += Unit_OnTurnChange;
        currentPosition = LevelGrid.Instance.GetGridPosition(transform.position);
        LevelGrid.Instance.AddUnitAtPosition(currentPosition, this);
    }
    private void Update(){
       
        GridPosition newPosition = LevelGrid.Instance.GetGridPosition(transform.position);
        if(newPosition != currentPosition){
            LevelGrid.Instance.ClearUnitAtPosition(currentPosition, this);
            currentPosition = newPosition;
            LevelGrid.Instance.AddUnitAtPosition(currentPosition, this);
        }
    }
    private void Unit_OnTurnChange(object sender, EventArgs e){
        if((isEnemy && !TurnSystem.Instance.IsPlayerTurn()) ||
            (!isEnemy && TurnSystem.Instance.IsPlayerTurn())){
            actionPoints = BaseActionPoints;
        }
        OnAnyActionPointsChange.Invoke(this, EventArgs.Empty);
    }
    public Move getMove(){
        return move;
    }
    public Spin getSpin(){
        return spin;
    }
    public GridPosition getPosition(){
        return currentPosition;
    }
    public BaseAction[] GetActions(){
        return actionArray;
    }
    public bool TryTakeAction(BaseAction action){
        if(CanSpendActionPoints(action)){
            SpendActionPoints(action.GetCost());
            return true;
        }
        return false;
    }
    public bool CanSpendActionPoints(BaseAction action){
        return actionPoints >= action.GetCost();
    }
    private void SpendActionPoints(int cost){
        if(cost == 10){
            actionPoints = 0;
        }
        else{
            actionPoints -= cost;
        }
        OnAnyActionPointsChange.Invoke(this, EventArgs.Empty);
    }
    public int GetActionPoints(){
        return actionPoints;
    }
    public bool IsEnemy(){
        return isEnemy;
    }
    public void Damage(int damage){
        health.Damage(damage);
    }
    public Vector3 GetWorldPosition(){
        return transform.position;
    }
    private void Unit_onDeath(object sender, EventArgs e){
        LevelGrid.Instance.ClearUnitAtPosition(currentPosition, this);
        Destroy(gameObject);
    }
}

UnitSelected Class

public class UnitSelectedVisual : MonoBehaviour
{
    [SerializeField] private Unit selected;
    private MeshRenderer mesh;
    private void Awake(){
        mesh = GetComponent<MeshRenderer>();
    }
    private void Start(){
        UnitActionSystem.Instance.selectedUnitChange += UnitActionSystem_selectedUnitChange;
        updateVisual();
    }
    private void UnitActionSystem_selectedUnitChange(object sender, EventArgs empty){
        updateVisual();
    }
    private void updateVisual(){
        if(UnitActionSystem.Instance.GetSelectedUnit() == selected){
            mesh.enabled = true;
        }
        else{
            mesh.enabled = false;
        }
    }
    private void OnDestroy(){
        UnitActionSystem.Instance.selectedUnitChange -= UnitActionSystem_selectedUnitChange;
    }
}

How do you know there’s an error if the console doesn’t tell you? Just need some clarity because if the console tells you there’s an error, it also tells you where it is. If you can show the error it may help track down the issue

If you click on an Error message in the console, the details of the error appear at the bottom of the console. This is known as a Stack Trace. That information is pure gold for debugging because it shows the method and line that the error occurred in and the call stack of methods that called the method. Pasting that Stack Trace in, even as a screenshot can be invaluable to letting us help you figure out the bug.

Privacy & Terms