Script only picks last object from an array, instead of the closest object

As the post suggests, I’m working on a code that selects a couple different object as fixed destination points. It’s supposed to allow for a creature to wander about in random directions (Which that works nicely), and takes whatever object is closest and sets that as the destination whenever needed.
For example:
“Hey! I’m getting thirsty! Oh look, a pond is near by, I’ll go there and drink!”
Instead what I get is:
“Hey! I’m getting thirsty! I remember seeing a lake 2 miles away, I’ll walk there instead of this lovely one next to me. Everytime.”

My code, though not the prettiest, goes as follows

var Thirst : float = 100;
var Hunger : float = 100;
var Sleep : float = 100;
var Health : float = 100;
var WaterSource : GameObject[];
var Nests : GameObject[];
var FoodSource : GameObject[];
var HomeNest : GameObject;
var WaterBlock : GameObject;
var FoodBlock : GameObject;
var WaterTarget : Vector3;
var HomeTarget : Vector3;
var FoodTarget : Vector3;
var ShowDistance : float;
var Dead : boolean;
private var WanderRepeat : float;
private var WanderInterval : int;
var Radius : int;
var Speed : float;
private var Wait : float;


function Start()
{
	WaterSource = GameObject.FindGameObjectsWithTag("Water");
	FoodSource = GameObject.FindGameObjectsWithTag("Food");
	Nests = GameObject.FindGameObjectsWithTag("Nest");
	SetHome();
}

function Update()
{
	Wait += Time.deltaTime;
	Thirst -= .01;
	Hunger -= .005;
	Sleep -= .001;
	var HomeDistance = Vector3.Distance(HomeTarget, transform.position);
	var WaterDistance = Vector3.Distance(WaterTarget, transform.position);
	var FoodDistance = Vector3.Distance(FoodTarget, transform.position);
	var Wander = Vector3.Distance(WanderPoint, transform.position);
	FindClosestWater();
	FindClosestFood();
	ShowDistance = HomeDistance;
	if(Dead == false)
	{
		if(Sleep < 50)
		{
			if(HomeDistance <= 10)
			{
			WanderPoint = Vector3(this.transform.position.x,this.transform.position.y ,this.transform.position.z);
			WanderRepeat = Wait + 10;
			animation.Play("Sleep");
			Sleep+= 30;
			}
			else{transform.LookAt(HomeTarget); transform.Translate(Vector3.forward*Speed*Time.deltaTime); animation.Play("Run");}
		}
		else if(Thirst <=50)
		{
			if(WaterDistance<=4)
			{
				WanderPoint = Vector3(this.transform.position.x,this.transform.position.y ,this.transform.position.z);
				WanderRepeat = Wait + 10;
				animation.Play("Drinking");
				Thirst+= 40;

			}
			else{transform.LookAt(WaterTarget); transform.Translate(Vector3.forward*Speed*Time.deltaTime); animation.Play("Run");}
		}
		else if(Hunger < 50)
		{

			if(FoodDistance<=5)
			{
				WanderPoint = Vector3(this.transform.position.x,this.transform.position.y ,this.transform.position.z);
				WanderRepeat = Wait + 10;
				animation.Play("Drinking");
				Hunger += 50;
			}
			else{transform.LookAt(FoodTarget); transform.Translate(Vector3.forward*Speed*Time.deltaTime); animation.Play("Run");}
				
		}
		else
		{
			if(WanderRepeat < Wait)
			{
			MoveWanderPoint();
			}

			if(Wander >= 1.5f)
			{
				transform.LookAt(WanderPoint);
				transform.Translate(Vector3.forward*Speed*Time.deltaTime);
				animation.CrossFade("Run");
				
			}
			else
			animation.CrossFade("Idle");
		}
	}
	if(Sleep <= 25 || Thirst <= 10 || Hunger <=5)
	Health--;
	
	if(Health <= 0)
	{Dead = true; //Dying();}
	
	
	}
}

function MoveWanderPoint()
{
	var RanX : float = Random.Range(-Radius, Radius);
	var Ranz : float = Random.Range(-Radius, Radius);
	WanderInterval = Random.Range(5,15);
		
	WanderRepeat = Wait + WanderInterval;
	WanderPoint = Vector3(HomeTarget.x+RanX, 0.9, HomeTarget.z+Ranz);
	
}

function FindClosestWater()
{
	var WaterBlock : GameObject;
	var distance = Mathf.Infinity;
	var position = transform.position;
	var go : GameObject;
	for(go in WaterSource)
		var diff = (go.transform.position - position);
		var curDistance = diff.sqrMagnitude;
			if(curDistance < distance)
			{
				WaterBlock = go;
				distance = curDistance;
				
			}
	WaterTarget = Vector3(WaterBlock.transform.position.x, this.transform.position.y, WaterBlock.transform.position.z);
}

function FindClosestFood()
{
	var FoodBlock : GameObject;
	var distance = Mathf.Infinity;
	var position = transform.position;
	var go : GameObject;
	for(go in FoodSource)
		var diff = (go.transform.position - position);
		var curDistance = diff.sqrMagnitude;
			if(curDistance < distance)
			{
				FoodBlock = go;
				distance = curDistance;
				
			}
	FoodTarget = Vector3(FoodBlock.transform.position.x, this.transform.position.y, FoodBlock.transform.position.z);
}

function SetHome()
{
	var distance = Mathf.Infinity;
	var position = this.transform.position;
	var go : GameObject;
	for(go in Nests)
		var diff = (go.transform.position - position);
		var curDistance = diff.sqrMagnitude;
			if(curDistance < distance)
			{
				HomeNest = go;
				distance = curDistance;
				
			}
	HomeTarget = Vector3(HomeNest.transform.position.x, this.transform.position.y, HomeNest.transform.position.z);
}

If the format looks familiar, yes, I did use the example from the scripting API, and I’m basically garunteeing that’s atleast part of my problem, but I don’t understand arrays at all and I don’t know why… So~! Throw me a bone if you will, it really sucks having a really long code only work a quarter of the way.

Please and Thank You!

Update!

I ran some more debugging scripts through this, and found out that it’s picking the last object in the array instead of the closest object.

function FindClosestWater()
{
var distance = Mathf.Infinity;
var position = transform.position;
var go : GameObject;
for(go in WaterSource)
var diff = (go.transform.position - position);
var curDistance = diff.sqrMagnitude;
if(curDistance < distance)
{
WaterBlock = go;
distance = curDistance;
WaterTarget = Vector3(WaterBlock.transform.position.x, this.transform.position.y, WaterBlock.transform.position.z);
}

}

This should chose the closest “water block” from the array
WaterSource = GameObject.FindGameObjectsWithTag(“Water”);
The array is populated properly, but it always checks the last item in the array instead, no matter the size of the array.

Wow…
Okay, So, I had the script almost entirely accurate but I did miss something really obvious!

in the tutorial in the Scripting API, after the for statement there aren’t any curly brackets.

So… looking that over I fixed the curly brackets, reset the calling for “go”, and moved the detection for each of the blocks, and it works just fine now…

function FindClosestWater()
{
	var distance = Mathf.Infinity;
	var position = transform.position;
	for(var go : GameObject in WaterSource)
	{
		var diff = (go.transform.position - position);
		var curDistance = diff.sqrMagnitude;
			if(curDistance < distance)
			{
				WaterBlock = go;
				distance = curDistance;
				WaterTarget = Vector3(WaterBlock.transform.position.x, this.transform.position.y, WaterBlock.transform.position.z);
			}
		}
	
}