Move to closest object(c#)

I’ve seen a couple of answers in java script but I don’t quite understand them so here goes:

I’m trying to get my squirrel model to move to the closest pine cone. It seems there is a fundamental problem with my script as many times I see the squirrel run past a pine cone right next to it and go accross the screen. When I added five squirrels, they all went for the same cone every time. Here’s the script:

	public void LookForFood()
	{
		canMove = true;
		eatingTimer = 5; 
		if(managerScript.pineCones != null)
		{
			for(int i = 0; i < managerScript.pineCones.Count; i++)
			{
				if(Vector3.Distance(transform.position, managerScript.pineCones*.transform.position) < Vector3.Distance(transform.position, managerScript.checkForDistancePineCone))*

_ closestPineCone = managerScript.pineCones*;_
_ target = managerScript.pineCones.transform.position;
}*_

* Quaternion lookAtPineCone = Quaternion.LookRotation(closestPineCone.transform.position - transform.position);*
* transform.rotation = Quaternion.Slerp(transform.rotation, lookAtPineCone, Time.deltaTime);*

* gameObject.transform.FindChild(“SquirrelModel”).animation.CrossFade(“SquirrelLookFood”);*

* if(gameObject.transform.FindChild(“SquirrelModel”).animation[“SquirrelLookFood”].time > 2.2f)*
* {*
* findTarget = true;*
* squirrelBehaviour = SquirrelBehaviour.MoveToFood;*
* }*
* }*
* else if (managerScript.pineCones == null)*
* {*
* squirrelBehaviour = SquirrelBehaviour.FindRandomTree;*
* }*

* }*

ClosestPineCone is stored in the manager script where the pine cone list is kept. I set this to the first element of the list just as a random check. My hope was that it would check every element of the list and keep comparing to find the shortest distance. I susepct it’s breaking out before it does this I can’t work out why.

I think it’s the ‘if statement’ on line 9, there are no curly brackets so only the next line is included in the if statement. Is that deliberate?

ie. currently target is set at every loop through the for loop therefore its final value is always the last item in managerScript.pineCones.

My mistake (apart from the brackets typo above!) was that I was not changing the value of the checkForDistancePineCone variable -which is really just the first element in the pine cone list. So the for loop would just find the first transform that was closer than pineCones[0] and leave it at that. By changing the checkForDistancePineCone variable to the transform closer to the squirrel, the for loop actually finds the closest object now. Here is the amended code for anyone interested:

	for(int i = 0; i < managerScript.pineCones.Count; i++)
			{
				if(Vector3.Distance(transform.position, managerScript.pineCones*.transform.position) < Vector3.Distance(transform.position, managerScript.checkForDistancePineCone))*
  •  		{*
    

_ managerScript.checkForDistancePineCone = managerScript.pineCones*.transform.position;_
_ closestPineCone = managerScript.pineCones;
}
}*_

* target = closestPineCone.transform.position;*