Enemy AI passing through walls

Though I have some tutorials and answers regarding enemy passing through walls but still didn’t get it how to stop it? And so now I am attaching scripts. I know something should be added around the setdestination line but what??

void Update()
{
    float distance = Vector3.Distance(target.position, transform.position);  //x,y,z distance between target position 
                                                                             //and our position

    if (distance <= 60f)
    {
        agent.SetDestination(target.position);                  //enemy will start chasing our hero (animations here)

        if (distance <= agent.stoppingDistance)
        {
            FaceTheTarget();                                    //face the target

            if (Time.time >= nextFiringTime && bulletreloadtime>0)
            {
                ShootTheTarget();                                  //attack the target (animations here)
                nextFiringTime = Time.time + 1f / 7f;               //set timing intervals of enemy fire
                bulletreloadtime--;
            }
            if(bulletreloadtime==0)
            {
                bulletreloadtime = 40;              //bullets are reloaded
                nextFiringTime += 2.5f;             //bullet is reloading, time for reloading
            }
        }
    }
}

This doesn’t sound like a code problem. Check the walls are baked into the navmesh in the Windows->Navigation window.
Select the walls and check the Object tab.