Null reference exception

At 2:16 I’m getting an error.

NullReferenceException: Object reference not set to an instance of an object
BaseAction.ActionComplete () (at Assets/Scripts/Actions/BaseAction.cs:49)
GrenadeAction.Update () (at Assets/Scripts/Actions/GrenadeAction.cs:16)

BaseAction line 49 refers to: onActionComplete();
GrenadeAction line 16 refers to: ActionComplete();

Here is the full code for both scripts.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GrenadeAction : BaseAction
{

    private void Update()
    {
        if (!isActive)
        {
            return;
        }

        ActionComplete();
    }
    public override string GetActionName()
    {
        return "Grenade";
    }

    public override EnemyAIAction GetEnemyAIAction(GridPosition gridPosition)
    {
        return new EnemyAIAction
        {
            gridPosition = gridPosition,
            actionValue = 0,
        };
    }

    public override List<GridPosition> GetValidActionGridPositionList()
    {
        GridPosition unitGridPosition = unit.GetGridPosition();

        return new List<GridPosition>
        {
            unitGridPosition
        };
    }

    public override void TakeAction(GridPosition gridPosition, Action onCompleteAction)
    {
        Debug.Log("Grenade Action!");
        ActionStart(onActionComplete);
    }
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public abstract class BaseAction : MonoBehaviour
{
    public static event EventHandler OnAnyActionStarted;
    public static event EventHandler OnAnyActionCompleted;

    protected Unit unit;
    protected bool isActive;
    protected Action onActionComplete;

    protected virtual void Awake()
    {
        unit = GetComponent<Unit>();
    }

    public abstract string GetActionName();

    public abstract void TakeAction(GridPosition gridPosition, Action onCompleteAction);

    public virtual bool IsValidActionGridPosition(GridPosition gridPosition)
    {

        List<GridPosition> validGridPositionList = GetValidActionGridPositionList();
        return validGridPositionList.Contains(gridPosition);
    }

    public abstract List<GridPosition> GetValidActionGridPositionList();

    public virtual int GetActionPointsCost()
    {
        return 1;
    }

    protected void ActionStart(Action onActionComplete)
    {
        isActive = true;
        this.onActionComplete = onActionComplete;

        OnAnyActionStarted?.Invoke(this, EventArgs.Empty);
    }

    protected void ActionComplete()
    {
        isActive = false;
        onActionComplete();

        OnAnyActionCompleted?.Invoke(this, EventArgs.Empty);
    }

    public Unit GetUnit()
    {
        return unit;
    }

    public EnemyAIAction GetBestEnemyAIAction()
    {
        List<EnemyAIAction> enemyAIActionList = new List<EnemyAIAction>();
        List<GridPosition> validActionGridPositionList = GetValidActionGridPositionList();

        foreach (GridPosition gridPosition in validActionGridPositionList)
        {
            EnemyAIAction enemyAIAction = GetEnemyAIAction(gridPosition);
            enemyAIActionList.Add(enemyAIAction);
        }

        if (enemyAIActionList.Count > 0)
        {
            enemyAIActionList.Sort((EnemyAIAction a, EnemyAIAction b) => b.actionValue - a.actionValue);
            return enemyAIActionList[0];
        } else
        {
            // No possible enemy AI actions
            return null;
        }
    }

    public abstract EnemyAIAction GetEnemyAIAction(GridPosition gridPosition);

I think I found the problem. On my BaseAction class, the Abstract TakeAction had 2nd parameter for Action onCompleteAction. On other scripts I was using onActionComplete. I’m not sure why this wasn’t an issue before now, but once I changed the abstract and the override to onActionComplete it seems to be working.

1 Like

This was your culprit, and the reason that changing the overrride worked. In the header of TakeAction, you have onCompletedAction, but when you call ActionStart, you’re using onActionComplete, which winds up being the protected onActionComplete that hasn’t been set yet.

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

Privacy & Terms