Unity crashes when I press the right mouse button and a variable is equal to 1

I am making an RTS and the basics of my movement system is that each unit has a variable calles isSelected. When that unit is clicked that variable is set to 1 and when the mouse is right clicked anywhere else it moves all units with the variable of 1 to that point.

The problem is that when I right click and have a unit selected Unity completely freezes and I have to end process from the task manager.

using UnityEngine;
using System.Collections;

public class UnitControl : MonoBehaviour {
	public GameObject[] Units;
	// Use this for initialization
	void Start () 
	{




	}

	// Update is called once per frame
	void Update () 
	{
		float moveSpeed = 5.1f;
		if (Input.GetMouseButtonDown (1)) 
		{
			Units = GameObject.FindGameObjectsWithTag("Unit");
			Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
			RaycastHit hit = new RaycastHit();
			if (Physics.Raycast (ray, out hit))
			{
				Vector3 movePos = new Vector3 (hit.point.x, 1.5f, hit.point.z);
				for (int i = 0; i < Units.Length; i++) 
				{
					GameObject Unit = GameObject.Find(Units*.name);*
  •  			isSelectedScript isSelectedScript = Unit.GetComponent<isSelectedScript>();*
    
  •  			Debug.Log("Debug working before isSelected loop");*
    
  •  			if (isSelectedScript.isSelected == 1)*
    
  •  			{*
    
  •  				Debug.Log("This is between the if and the while loops");*
    
  •  				while (Vector3.Distance(Unit.transform.position, hit.point) > 0.5f)*
    
  •  				{*
    
  •  					Vector3.MoveTowards(transform.position, hit.point, 2.0f);*
    
  •  				}*
    
  •  			}*
    
  •  		}*
    
  •  	}*
    
  •  }*
    
  • }*
    }

This is the code for moving the unit to the click position. I’ve got it to the point where it WON’T crash if the while loop at the bottom is commented out but as soon as I uncomment it then it will crash again. Can anyone offer any help with this? I have had this problem for about 2 weeks and I really can’t figure out why.

The loop in lines 37-40 will never stop because you aren’t using the correct start position and never assign the new position. The loop should probably read

while (Vector3.Distance(Unit.transform.position, hit.point) > 0.5f)
{
    Unit.transform.position=Vector3.MoveTowards(Unit.transform.position, hit.point, 2.0f);
}