Piece of code including foreach loop not working correctly

Everytime I press play, “Distance” is always 0… What am I doing wrong?

if (DataValue == CubeManagerGet.GetComponent <CubeManager> ().CurrentCube)
		{
			TextGet.text = "Coordinates: {" + transform.position.x + ", " + transform.position.y + "}";
			Debug.Log ("YES");
			GameObject [] Cubes = GameObject.FindGameObjectsWithTag ("Cube");
			float Distance = Mathf.Infinity;
			foreach (GameObject CubeTag in Cubes)
			{
				if (Vector3.Distance (transform.position, CubeTag.transform.position) < Distance)
				{
					Distance = Vector3.Distance (transform.position, CubeTag.transform.position);
				}
				if (Distance <= 5)
				{
					CanSubtract = true;
				}
				else
				{
					CanSubtract = false;
				}
			}
			Debug.Log (Distance);
		}

Why this statement?

if (Vector3.Distance (transform.position, CubeTag.transform.position) < Distance)

Is any of your cube at position of the object the script is attached to? Remove the above check.

Edit your loop as following:

foreach (GameObject CubeTag in Cubes)
{
      Distance = Vector3.Distance (transform.position, CubeTag.transform.position);
	if (Distance <= 5)
	{
	     CanSubtract = true;
        }
	else
	{
	     CanSubtract = false;
	}
     Debug.Log(Distance);
}

I went through your code and fixed a couple of things,
I removed something that you need to put back (only because i don’t have the whole code, so for instance the first if statement, and the CanSubtract which is now a simple Debug (just put back your own code).

Although i don’t really understand what you’re trying to do, here’s the code that worked and let me know if you need anything else, Distance is successfully changing and the if tests are working.
Thanks for voting up and accepting as “Answered”.

using UnityEngine;
using System.Collections;

public class HasMoved : MonoBehaviour {

	public GameObject [] Cubes;
	float Distance;

	void Start(){
		Debug.Log( "Coordinates: {" + transform.position.x + ", " + transform.position.y + "}");
		Cubes = GameObject.FindGameObjectsWithTag ("Cube");

		Distance = Mathf.Infinity;
	}

	void Update(){
		foreach (GameObject CubeTag in Cubes)
		{
			if (Vector3.Distance (transform.position, CubeTag.transform.position) < Distance)
			{
				Distance = Vector3.Distance (transform.position, CubeTag.transform.position);
			}
			if (Distance <= 5)
			{
				Debug.Log("Can Subtract True");
			}
			else
			{
				Debug.Log("Can Subtract False");
			}
		}
		Debug.Log (Distance);
	}

}