How to make enemy move through walls?

I am working on an enemy that should only move when the player is not looking at it. I tried using the Renderer.isVisible and everything works but if I look in the direction of the enemy through a wall he also stops moving. The isVisible method counts it in camera view even if a wall is between the player and the enemy. I also tried OnBecameVisible()/OnBecameInvisible() but I get the same result. Does anyone know how to solve it? Here is the code:

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

public class AI : MonoBehaviour
{
    public GameObject deathScreen;
    public UnityEngine.AI.NavMeshAgent agent;
    public Animator endo;
    public Transform player;

    public LayerMask whatIsGround, whatIsPlayer;
    
    //Patroling
    public Vector3 walkPoint;
    bool walkPointSet;
    public float walkPointRange;

    //Attacking
    public float timeBetweenAttacks;
    bool alreadyAttacked;
    bool isDead;

    //States
    public float sightRange, attackRange;
    public bool playerInSightRange, playerInAttackRange;

    private void Awake()
    {
        player = GameObject.Find("Player").transform;
        agent = GetComponent<UnityEngine.AI.NavMeshAgent>();
    }

    private void Update()
    {
        if(agent.GetComponent<Renderer>().isVisible)
        {
            endo.speed = 0f;
            agent.speed = 0f;
        }
        else
        {
            endo.speed = 1f;
            agent.speed = 10f;
        }
        //Check for sight and attack range
        playerInSightRange = Physics.CheckSphere(transform.position, sightRange, whatIsPlayer);
        playerInAttackRange = Physics.CheckSphere(transform.position, attackRange, whatIsPlayer);

        //if (!playerInSightRange && !playerInAttackRange) Patroling();
        if (playerInSightRange && !playerInAttackRange) ChasePlayer();
        if (playerInAttackRange && playerInSightRange) AttackPlayer();
    }

    private void Patroling()
    {
        if (!walkPointSet) SearchWalkPoint();

        if (walkPointSet)
            agent.SetDestination(walkPoint);

        Vector3 distanceToWalkPoint = transform.position - walkPoint;

        //Walkpoint reached
        if (distanceToWalkPoint.magnitude < 1f)
            walkPointSet = false;
    }
    private void SearchWalkPoint()
    {
        //Calculate random point in range
        float randomZ = Random.Range(-walkPointRange, walkPointRange);
        float randomX = Random.Range(-walkPointRange, walkPointRange);

        walkPoint = new Vector3(transform.position.x + randomX, transform.position.y, transform.position.z + randomZ);

        if (Physics.Raycast(walkPoint, -transform.up, 2f, whatIsGround))
            walkPointSet = true;
    }

    private void ChasePlayer()
    {
        endo.SetBool("Walk", true);
        agent.SetDestination(player.position);
    }

    private void AttackPlayer()
    {
        //Make sure enemy doesn't move
        agent.SetDestination(transform.position);

        transform.LookAt(player);

        if (!alreadyAttacked)
        {
            if(isDead == true)
            {
                FindObjectOfType<Movement_Player>().sensitivity = 0f;
                Time.timeScale = 0f;
                Cursor.lockState = CursorLockMode.None;
                return;
            }
            ///Attack code here
            Instantiate(deathScreen, new Vector3(0f,0f,0f), Quaternion.identity);
            isDead = true;
            //FindObjectOfType<AudioController>().Play("AIShoot");
           
            ///End of attack code

            alreadyAttacked = true;
            Invoke(nameof(ResetAttack), timeBetweenAttacks);
        }
    }
    private void ResetAttack()
    {
        alreadyAttacked = false;
    }

    private void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(transform.position, attackRange);
        Gizmos.color = Color.yellow;
        Gizmos.DrawWireSphere(transform.position, sightRange);
    }
}

isVisible works by looking if the bounds of the object to be displayed are inside the frustum of the camera. if you would have a lot of those enemies you could add the OnBecameVisible and OnBecameInvisible to add to a list an then do a raycast to all objects in the list to know if they’re seen.