How can i check if one object gets near any object of a prefab

Hi im trying to make a simple code so a object hides when it comes near a trench, the problem is that I have many trenches and the way I’ve done it it only focuses the first one, but i want it to focus all the objects of a prefab. Please help, im new on this and i could only think of doing it this way:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AIsoldier : MonoBehaviour {
public float speed = 0.1f;
public Transform Trench;
public float DistanceToTrench;
public bool InTrench;
// Use this for initialization
void Start () {
}

// Update is called once per frame
void Update() {
    DistanceToTrench = Vector2.Distance(transform.position, Trench.position);

    Vector2 position = this.transform.position;
    position.x += speed;
    if (DistanceToTrench < 0.1 && InTrench == false)
    {
        InTrench = true;
        speed = 0;
        position.y += -5f;
    }
    else if (Input.GetKeyDown(KeyCode.Space) && InTrench==true)
    {
        InTrench = false;
        speed = 0.1f;
        position.y += 5f;
        position.x += 0.1f;
    }
    this.transform.position = position;
}

}

You could have this a little easier. Add a spherecollider as trigger to your trenches. Add OnTriggerEnter/ Exit to your code. Whenever you enter one, you count an int up, when you exit one, you count down. When the count is 0, you’re in no range and vice versa. Easy.

Use the Vector2.Distance() with your object and other objects of prefab. Result will be distance between them. Check the result and determine nearness.

  public Transform Trench1;
  public Transform Trench2;
  public float dist1;
  public float dist2;

  void Update() {
    dist1 = Vector2.Distance(transform.position, Trench1.position);
    dist2 = Vector2.Distance(transform.position, Trench2.position);
    if (dist1 < 1f) {
      // Do something
    }
    if (dist2 < 1f) {
        // Do something
    }
  }