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);