Counting number of blocks in an area

Title sums it up. I have been working on random generation lately. I have made it so when water is generated, there is a chance that a “water checker” is spawned. What I want the water checker to do is count the amount of water blocks in the area to see if it is suitable for fish to spawn. Now I know how to spawn fish and all, but how would I start at a script that finds blocks with the name/Tag “Water” in a certain Radius and count them? So far I have this:

var array : Array;
var NumberOfWaterBlocks = 0;

function Update(){
array = GameObject.FindGameObjectsWithTag("Water");
print(array);
NumberOfWaterBlocks = array.length;
}

So far my script counts the total amount of objects with the tag “Water” But, I only want to do it in a certain radius, around the “water checker”. How would I start this? Can someone steer me in the right direction. Thanks.

One of the simplest way is to modify your script and calculate distance between center of randius and each found game object. After that you should remove object from the result collection if the distance greater than radius.

List<GameObject> result = new List<GameObject>();
foreach(GameObject foundGameObject in array)
{
  if(Vector3.Distance(foundGameObject.transform.position, radius.transform.position)< radius)
  {
    result.Add(foundGameObject);
  }
}

another simple way to do counting of near by objects is to have an empty game object with a primitive collider on it and set it as a trigger and then you scale that collider to match your area of detection and then check for any object with tag “water” that entered the trigger using ontriggerenter function and then increment a counter.