AI Sprite will not change

I have AI’s wondering around my 2D game but they will not change sprites. I have 3 sprites in an array. One looking left, right, and looking strait forward. What needs to happen is the AI moves left the sprite looks left, moves right looks right and so forth.

Here is my code.

    public Transform[] targets;//were the AIs will go...
    public Sprite[] People;
    public SpriteRenderer mySprite;//the referance for the SpriteRenderer
    float posXAI;//X Position of object
    float posYAI;//Y position of object
    int index;//which is the next target
    IAstarAI agent;//get infomation on how to pathfind...

    void Awake()
    {
        posXAI = transform.position.x;//get X positon of sprite...
        posYAI = transform.position.y;//get Y psotion of sprite...
        mySprite.gameObject.GetComponent<SpriteRenderer>();
        agent = GetComponent<IAstarAI>();
        //Debug.Log(posXAI);
        //Debug.Log(posYAI);
    }
    void Update()
    {
        MoveAI();
        ChangeSpite();
    }
    void ChangeSpite()
    {
        if (posXAI < transform.position.x)
        {
            mySprite.gameObject.GetComponent<SpriteRenderer>().sprite = null;
            mySprite.gameObject.GetComponent<SpriteRenderer>().sprite = People[1];
            Debug.Log(People[1]);
        }
        if (posXAI > transform.position.x)
        {
            mySprite.gameObject.GetComponent<SpriteRenderer>().sprite = People[2];
        }
        if (posYAI < transform.position.y)
        {
            mySprite.gameObject.GetComponent<SpriteRenderer>().sprite = People[0];
        }
        if (posYAI > transform.position.y)
        {
            mySprite.gameObject.GetComponent<SpriteRenderer>().sprite = People[0];
        }
    }

I thank anyone that comes along to help me. I’ve been working on this for a few days and I’m completely stumped.

OK so I finally got it. This took way too long but it at a point were I’m happy with it and it dose what I want to do. Look in the direction it’s moving.

You need A* Pathfinding to fully use the script but this can be used for other types of 2D movement.

Here is the full script.

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

public class PedAI : MonoBehaviour {
    /*Too make this script work in any way you must...
	--> Place Seeker Script, and AIPath or Lerp or RichAI Script on object...
    --> DO NOT PUT THIS AS CHILED OF ANY GAMEOBJECT...
    --> THIS WILL MAKE A* NOT WORK...
	*/
    public Transform[] targets;//were the AIs will go...
    public Sprite Person1;
    public Sprite Person2;
    public Sprite Person3;
    public SpriteRenderer mySprite;//the referance for the SpriteRenderer
    int index;//which is the next target
    IAstarAI agent;//get infomation on how to pathfind...

    void Awake()
    {
        mySprite.GetComponent<SpriteRenderer>().sprite = Person1;
        agent = GetComponent<IAstarAI>();
    }
    void Update()
    {
        MoveAI();
        ChangeSpite();
    }
    void ChangeSpite()
    {
        if (agent.velocity.x <= -1)
        {
            //Debug.Log(agent.velocity.x + "isLeft");
            mySprite.sprite = Person1;
            mySprite.flipX = true;
        }
        if (agent.velocity.x >= 1)
        {
            //Debug.Log(agent.velocity.x + "isRight");
            mySprite.sprite = Person1;
            mySprite.flipX = false;
        }
        //there is only one sprite for now... 
        //when I make another one it will look better... 
        if (agent.velocity.y <= -1)
        {
            Debug.Log(agent.velocity.y + " isDown");
            mySprite.sprite = Person3;
        }
        if (agent.velocity.y >= 1)
        {
            Debug.Log(agent.velocity.y + " isUp");
            mySprite.sprite = Person3;
        }
    }
    void MoveAI()
    {
        if (targets.Length == 0) return;
        bool search = false;
        // Check if the agent has reached the current target.
        // We must check for 'pathPending' because otherwise we might
        // detect that the agent has reached the *previous* target
        // because the new path has not been calculated yet.
        if (agent.reachedEndOfPath && !agent.pathPending)
        {
            //index = index + 1;
            index = Random.Range(0, targets.Length);
            search = true;
        }
        // Wrap around to the start of the targets array if we have reached the end of it
        index = index % targets.Length;
        agent.destination = targets[index].position;
        // Immediately calculate a path to the target.
        // Note that this needs to be done after setting the destination.
        if (search) agent.SearchPath();
    }
}