I have a coroutine that when called it instantly exits even before it gets to the first statement. The object that it is attached to does not get destroyed. The coroutine is called from a method that is then called from another script. The scripts movingTroops and navigate are on separate gameObjects.
Here is the script that has the coroutine in it:
public class navigate : MonoBehaviour
{
public void callMoveTo(Vector3 target)
{
StartCoroutine("MoveTo", target);
}
public IEnumerable MoveTo(Vector3 target)
{
while (Vector3.Distance(transform.position, target) > .5 && movingTroops.selectedUnits.Contains(gameObject))
{
//code
yield return 0;
}
}
Here is the script that calls the callMoveTo script:
public class movingTroops : MonoBehaviour {
public Vector3 target;
public static List<GameObject> selectedUnits = new List<GameObject>();
void Update () {
if (Input.GetMouseButtonDown(1))
{
RaycastHit position;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out position))
{
target = new Vector3();
target = position.point;
for (int i = 0; i < selectedUnits.Count; i++)
{
if (selectedUnits *!= null)*
{
GameObject navobject = selectedUnits*;*
var nav = navobject.GetComponent();
nav.callMoveTo(target);
}
}
}
}
}
}