Box collider help

Hi all, I know this is a vague question, but I didn’t know what else to name it.

I have a monster that is supposed to follow the player when the player is in range of the box collider attached to the monster. When in the range of the box collider i want the monster to follow the player unity the player is out of range (the monster will still chase the player for ten seconds, and then start to randomly move if the player is not found)

the problem is that the monster only follows the player if the player is in range of the box collider, it doesnt wait ten seconds like i want it to.
private void OnTriggerEnter(Collider player)//when the box collider is seen
Debug.Log(“i see you”);//play a sound?


    IEnumerator VisibilityTime()
         transform.position = Vector3.MoveTowards(transform.position, target.transform.position,
             Time.deltaTime * speed);
         yield return new WaitForSeconds (10);
       //check if you can see still see
       //only works if in box collider is still in view

I think you should constantly follow the player. At the moment it looks like you are only following onces at the begin of the coroutine. One way is probably to do it in Update.
Here’s a short (not tested) approach:

Transform target;

void Update(){

private Follow(){
    if(target != null){
        transform.position = Vector3.MoveTowards(transform.position, target.position, Time.deltaTime * speed);

private void OnTriggerEnter(Collider player){
    target = player.transform;

IEnumerator VisibilityTime(){    
    yield return new WaitForSeconds(10f);
    target = null;

It’a just a rough idea. Maybe you also have to check, when you enter the trigger again, if the coroutine is already running at the moment, and if yes, then restart it, so that you again have to wait for 10 seconds.