Wandering Code Not Working When Player Hides

I made an AI for my game, and the player has the ability to hide from said AI in places like desks and closets. The AI works fine in any other situation. However, when the player hides, the AI is supposed to give up searching and start wandering once the AI gets close to where it last saw the player. Instead, it just stops in front of the last seen position and doesn’t move until it sees the player again. I’ve tried using a Coroutine and using a custom function for the wandering, but it doesn’t work. If anyone could help, that’d be appreciated greatly.

The hiding code

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

public class hide : MonoBehaviour
{

public bool hidden;
bool canHide;

void Update()
{

    playerController controller = GetComponent<playerController>();

    if (controller.crouching && canHide)
    {
        hidden = true;
    }

    if (!controller.crouching || !canHide)
    {
        hidden = false;
    }

}

void OnTriggerStay(Collider Col)
{
    if (Col.gameObject.tag == "Hiding Spot")
    {
        canHide = true;
    }
}

void OnTriggerExit(Collider Col)
{
    if (Col.gameObject.tag == "Hiding Spot")
    {
        canHide = false;
        hidden = false;
    }
}

}

The enemy AI
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class robotNavigation : MonoBehaviour
{

public float vision;
NavMeshAgent agent;

Transform player;

public float mapBorderX;
public float mapBorderY;

public Transform wanderTarget;

float timer = 1.81f;

public bool inAttackRange;
bool wandering;

Vector3 lastPos;

void Awake()
{
    agent = GetComponent<NavMeshAgent>();
    player = GameObject.FindWithTag("Player").transform;
}

void Update()
{
    float distance = Vector3.Distance(transform.position, player.position);
    hide hidden = player.gameObject.GetComponent<hide>();

    if (distance <= vision)
    {
        //Shoots a linecast between the players position and itself.
        if (Physics.Linecast(transform.position, player.position) && !hidden.hidden)
        {
            /*If the linecast is not blocked, sets the agents destination to the players position,
            and records where the player was last seen, as well as resetting the move timer and stopping the wander loop.*/
            wandering = false;
            agent.SetDestination(player.position);
            lastPos = player.position;
            timer = 1.81f;
        }

        /*If the player is hidden, goes to the last known position and then wanders if it can't find the player.*/
        if (hidden.hidden)
        {
            
            float dist = Vector3.Distance(transform.position, lastPos);
            agent.SetDestination(lastPos);

            if (dist <= .02f)
            {
                wandering = true;
            }
        }

        /*Otherwise, sets the destination to last known player position, and gets a float for distance from the last seen position. If the distance from the
        last seen position is less than 0.2f, starts the wander loop.*/
        else
        {
            float dist = Vector3.Distance(transform.position, lastPos);
            agent.SetDestination(lastPos);

            if (dist <= .02f)
            {
                wandering = true;
            }
        }
    }

    //If the distance from the player is less than or equal to the agents stopping distance, sets inAttackRange to true.
    if (distance <= agent.stoppingDistance)
    {
        inAttackRange = true;
    }

    //If the distance from the player is greater than the agents stopping distance, sets inAttackRange to false.
    if (distance > agent.stoppingDistance)
    {
        inAttackRange = false;
    }

    if (wandering)
    {
        agent.SetDestination(wanderTarget.position);
        timer -= Time.deltaTime;
    }

    //If the player is outside of the vision radius, start the wander loop.
    if (distance > vision)
    {
        wandering = true;
    }

    //If the timer is less than or equal to 0, get a new wander destination, and start the timer again.
    if (timer <= 0f)
    {
        wanderTarget.position = new Vector3(Random.Range(-mapBorderX, mapBorderX), 0f, Random.Range(-mapBorderY, mapBorderY));
        timer = Random.Range(1.81f, 2.61f);
    }
}

//Display the vision radius in the editor.
void OnDrawGizmosSelected()
{
    Gizmos.color = Color.black;

    Gizmos.DrawWireSphere(transform.position, vision);
}

}

The bug:

Once again, thanks to whoever helps me with this, I’m very thankful!

When asking for help you can help us understand your problem even better if you describe what steps you have taken to narrow down your issue and try to figure out what is going on.

The first thing I would do is add a bunch of Debug.Log() to narrow down the problem area.

  1. Does wandering get set to true when the enemy stops? Does the enemy actually get within 0.02 meter from lasPos?
  2. Does the timer get down to 0 so it can set a wandering target?
  3. Is the target set a place that is possible for the navmesh agent to travel to?

This will help narrow down where things go wrong so we have an idea what to look for the thing that goes wrong.