Unable to Click on the Grid after moving the movement functionality

After the challenge to move the movement code, when I test it out I can select a unit but when I click on the Grid the unit does not move.

I have no compile errors at all
This is my UnitActionSystem.cs


private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            if (TryHandleUnitSelection()) { return; }
            selectedUnit.GetMoveAction().Move(MouseWorldPosition.GetPosition());
        }
    }

My MoveAction.cs


[SerializeField] private Animator unitAnimator;
    private Vector3 targetPosition;


    private void Awake()
    {
        targetPosition = transform.position;
    }

    // Update is called once per frame
    void Update()
    {
        float stoppingDistance = 0.1f;
        if (Vector3.Distance(transform.position, targetPosition) > stoppingDistance)
        {
            Vector3 moveDirection = (targetPosition - transform.position).normalized;
            float moveSpeed = 4f;
            transform.position += moveDirection * Time.deltaTime * moveSpeed;

            float rotateSpeed = 10f;
            transform.forward = Vector3.Lerp(transform.forward, moveDirection, Time.deltaTime * rotateSpeed);


            unitAnimator.SetBool("IsWalking", true);
        }
        else
        {
            unitAnimator.SetBool("IsWalking", false);
        }
    }

    public void Move(Vector3 targetPosition)
    {
        this.targetPosition = targetPosition;
    }

And the Unit.cs


public class Unit : MonoBehaviour
{
    private GridPosition gridPosition;
    private MoveAction moveAction;

    private void Awake()
    {
        gridPosition = GetComponent<GridPosition>();
        moveAction = GetComponent<MoveAction>();
    }


    private void Start()
    {
        gridPosition = LevelGrid.Instance.GetGridPosition(transform.position);
       
        LevelGrid.Instance.SetUnitAtGridPosition(gridPosition, this);
    }


    private void Update()
    {
        GridPosition newGridPosition = LevelGrid.Instance.GetGridPosition(transform.position);
        if(newGridPosition != gridPosition)
        {
            // Unit has changed it's gridPosition.
            LevelGrid.Instance.UnitMovedGridPosition(this, gridPosition, newGridPosition);
            gridPosition = newGridPosition;
        }
    }   


    public MoveAction GetMoveAction()
    {
        return moveAction;
    }
}


Any pointers much appreciated :slight_smile:

You mentioned no Compile errors, but were there any runtime errors?

Let’s add a few Debug.Logs
In UnitActionSystem before the selectedUnit.GetMoveAction().Move statement:

Debug.Log($"Initiating move to {MouseWorldPosition.GetPosition()}");

In MoveAction.Move:

Debug.Log($"Setting position to {targetPosition}");

Quick hint on formatting, the regular quotes (’’’’) don’t set things up for code. Instead, use the backwards apostrophes next to the 1 key (```). I edited the post for you to get the code formatted.

Privacy & Terms