How to stop enemy shooting through wall

Wrong Script i posted.

Its not LineCast… It should be:

if(Physics.Raycast(transform.position, target.position, hit)){
if(hit.transform == target){
return transform = target;

}
}

// In your script hit.transform and target will be the same if the raycast hits the target