Enemy AI not working correctly using state machine

so i made a state machine for my game but my problem is that when the enemy “sees” the player it switches from idle to attack skipping chase state and the enemy never moves. Also it doesn’t switch back to chase state when player is out of attack range.
this is my entire state machine:

State script:

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

public abstract class State : MonoBehaviour
{
    public abstract State RunCurrentState();
}

State Manager script:

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

public class StateManager : MonoBehaviour
{
    [SerializeField] private State currentState;

    void Update()
    {
        RunStateMachine();
    }

    private void RunStateMachine()
    {
        State nextState = currentState?.RunCurrentState();

        if (nextState != null)
        {
            SwitchToTheNextState(nextState);
        }
    }

    private void SwitchToTheNextState(State nextState)
    {
        currentState = nextState;
    }
}

Idle State script:

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

public class IdleState : State
{
    public ChaseState ChaseState;
    [SerializeField] private GameObject _target;
    [SerializeField][Range(0, 100)] private float _seenRange;
    private bool _isSpotted;
    private float _delta;

    private void Start()
    {
        _target = GameObject.Find("Player");
        _delta = _target.transform.localScale.x / 2f;
        StartCoroutine(CheckForTargets());
    }

    private IEnumerator CheckForTargets()
    {
        while (true)
        {
            yield return new WaitForSeconds(1);
            if (_target == null)
            {
                Debug.Log("No target...");
            }
            else
            {
                _isSpotted = false;
                RaycastHit2D[] seenEntities = Physics2D.RaycastAll(transform.position, _target.transform.position - transform.position, _seenRange);
                //Debug.DrawRay(transform.position, _target.transform.position - transform.position, Color.red, 1f, false);

                foreach (var ent in seenEntities)
                {
                    if (ent.transform.tag == _target.tag)
                    {
                        var ray2d = new Ray2D(_target.transform.position, transform.position - _target.transform.position); // enemies are not in world space
                        _isSpotted = true;
                        for (float i = 0; i < Vector3.Distance(_target.transform.position, transform.position); i += _delta)
                        {
                            var tile = MapManager.Instance.SteppedOnTile(Vector3Int.FloorToInt(ray2d.GetPoint(i)));
                            if (!tile.isWalkable)
                            {
                                _isSpotted = false;
                                break;
                            }
                        }
                    }
                }
            }
            if (_isSpotted)
            {
                gameObject.GetComponent<SpriteRenderer>().color = new Color(1, 0, 0);
            }
            else
            {
                gameObject.GetComponent<SpriteRenderer>().color = new Color(1, 1, 1);
                Debug.Log("a");
            }
            
        }
    }

    public void OnDrawGizmos()
    {
        //Gizmos.DrawLine(transform.position, _target.transform.position);
    }


    public override State RunCurrentState()
    {
        if (_isSpotted)
        {
            _isSpotted = false;
            StopAllCoroutines();
            return ChaseState;
        }
        else
        {
            return this;
        }
    }
}

Chase state:

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

public class ChaseState : State
{
    [SerializeField] private AttackState attackState;
    private bool isInAttackRange;
    [Range(0,100)] [SerializeField] private float attackRange = 10.0f;
    [SerializeField] private GameObject _target;
    private MovementComponent _movementScript;

    private void Start()
    {
        _movementScript = GetComponent<MovementComponent>();
        if (_target == null) _target = GameObject.Find("Player");
    }

    private void FixedUpdate()
    {
        var distance = Vector3.Distance(transform.position, _target.transform.position);
        isInAttackRange = distance <= attackRange;
    }
    public override State RunCurrentState()
    {
        if (isInAttackRange)
        {
            return attackState;
        }
        else
        {
            return this;
        }
    }
    private void ChasePlayer()
    {
        if (_target != null)
        {
            Vector3 direction = _target.transform.position - transform.position;
            //direction.Normalize();
            _movementScript.Move(direction);
        }
        else
        {
            Debug.LogError("Player doesnt exist :)");
        }
    }
}

and finally Attack state script:

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


public class AttackState : State
{
    [SerializeField] private Transform _target;
    private float attackRange = 5.0f;
    private ChaseState chaseState;
    private AttackComponent _attackScript;

    public AttackState(ChaseState chaseState)
    {
        this.chaseState = chaseState;
    }

    void Start()
    {
        _target = GameObject.Find("Player").transform;
        _attackScript = GetComponent<AttackComponent>();
    }

    public override State RunCurrentState()
    {
        if (_target != null && Vector3.Distance(transform.position, _target.position) <= attackRange)
        {
            _attackScript.Attack(transform.position - _target.position);
            return this;
        }
        else
        {
            switch (tag)
            {
                case "Enemy_Apple":
                    return chaseState;
                default:
                    return this;
            }
        }
    }
}

i’ve tried asking friends, chatgpt but when some issues were fixed other were made.

New contributor

Emilhhh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật