EnemyAI acts like move to position is hard coded (Unity Turn Based Strategy)

I dont really know how this ask a question works this is a problem for Unity Turn Based Strategy: Intermediate C# Coding episiode Enemy AI Complex and I don’t know if this will get posted there. Anyway as stated in the title the enemy units will always move or try to move as close as they can to GridPosition(0,0). The code is changed slightly from the videos but is mostly the same and I don’t even know where to start looking for the solution to this problem.

Any tips, ideas, random throughts, guid to positing this in the right place, etc. will be greatly appreciated

At a guess, the units are getting actions back from the AI that are reporting a GridPosition 0,0 instead of the actual location.

Let’s start with a look at your EnemyAI.cs script.

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

public class EnemyAI : MonoBehaviour
{

    private enum State
    {
        WaitingForEnemyTurn,
        TakingTurn,
        Busy,
    }

    private State state;

    private float timer;

    private void Awake()
    {
        state = State.WaitingForEnemyTurn;
    }

    private void Start()
    {
        TurnSystem.Instance.OnTurnChanged += TurnSystem_OnTurnChanged;
    }

    private void Update()
    {
        if (TurnSystem.Instance.IsPlayerTurn())
        {
            return;
        }

        switch (state)
        {
            case State.WaitingForEnemyTurn:
                break;
            case State.TakingTurn:
                timer -= Time.deltaTime;
                if (timer <= 0f)
                {
                    if (TryTakeEnemyAIAction(SetStateTakingTun))
                    {
                        state = State.Busy;
                    }
                    else
                    {
                        //no more enemies have actions they can take
                        TurnSystem.Instance.NextTurn();
                    }
                }
                break;
            case State.Busy:
                break;
        }


    }

    private void SetStateTakingTun()
    {
        timer = 0.5f;
        state = State.TakingTurn;
    }

    private void TurnSystem_OnTurnChanged(object sender, EventArgs e)
    {
        if (!TurnSystem.Instance.IsPlayerTurn())
        {
            state = State.TakingTurn;
            timer = 2f;
        }
    }

    private bool TryTakeEnemyAIAction(Action onEnemyAIActionComplete)
    {
        foreach (Unit enemyUnit in UnitManager.Instance.GetEnemyUnitList())
        {
            if(TryTakeEnemyAIAction(enemyUnit, onEnemyAIActionComplete))
            {
                return true;
            }
        }
        return false;
    }

    private bool TryTakeEnemyAIAction(Unit enemyUnit, Action onEnemyAIActionComplete)
    {
        EnemyAIAction bestEnemyAIAction = null;
        BaseAction bestBaseAction = null;

        foreach (BaseAction baseAction in enemyUnit.GetBaseActionArray())
        {
            if (!enemyUnit.CanSpendActionPointsToTakeAction(baseAction))
            {
                //Enemy cannot afford this action
                continue;
            }

            if (bestEnemyAIAction == null)
            {
                bestEnemyAIAction = baseAction.GetBestEnemyAIAction();
                bestBaseAction = baseAction;
            }
            else
            {
                EnemyAIAction testEnemyAIAction = baseAction.GetBestEnemyAIAction();
                if (testEnemyAIAction != null && testEnemyAIAction.actionValue > bestEnemyAIAction.actionValue)
                {
                    bestEnemyAIAction = testEnemyAIAction;
                    bestBaseAction = baseAction;
                }
            }
        }

        if(bestEnemyAIAction != null && enemyUnit.TrySpendActionPointsToTakeAction(bestBaseAction))
        {
            bestBaseAction.TakeAction(bestEnemyAIAction.gridPosition, onEnemyAIActionComplete);\
            return true;
        }
        else
        {
            return false;
        }
    }
}

here is the code, going through debugging it looks like this part:
EnemyAIAction testEnemyAIAction = baseAction.GetBestEnemyAIAction();

returns null everytime

just wondering but in the Unit script is the "

    baseActionArray = GetComponents<BaseAction>();"

supossed to return BaseAction[]?

Yes it is:

    public BaseAction[] GetBaseActionArray()
    {
        return baseActionArray;
    }

If you’re getting a null reference on the foreach statement, then check to make sure that your baseActionArray is getting initialized in Unit.Awake()?

    private void Awake()
    {
        healthSystem = GetComponent<HealthSystem>();
        baseActionArray = GetComponents<BaseAction>();
    }

Privacy & Terms