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