Debug.DrawLine inside Coroutine

I’ve got a coroutine that moves the GameObject to a preselected position and then stops, in this coroutine, I’m trying to show the distance to that position by using Debug.DrawLine(transform.position, target). The problem is, that the debug line doesn’t get rendered.

Heres is the coroutine code:

RigidBody2D rigidBody2D;
Vector2 target;

void Start () {
  rigidBody2D = GetComponent<RigidBody2D>();
  target = (Vector2)transform.position + Vecotr2.right;
  StartCoroutine(MoveHandler());
}

IEnumerator MoveHandler () {
  while (true) {
    Vector2 position = (Vector2)transform.position;
    Vector2 heading = target - position;
    float distance = heading.magnitude;

    if (distance < .2f || distance > 5f) yield return null;
    
    Vector2 direction = heading / distance;
    Vector2 newPos = position + (direction * speed * Time.deltaTime);
    
    Debug.DrawLine(position, target);
    rigidBody2D.MovePosition(newPos);

    yield return new WaitForEndOfFrame();
  }
}

Your code does work for me. The line is displayed as long as distance is greater than 5 or smaller than 0.2f. While the distance is in between those two limits it’s not rendered and this is the expected behaviour. If that’s not what you intended you should be more clear what you expect from that code.

Keep in mind that Debug.DrawLine is a debug utility. By default it won’t show up in the gameview, only in the sceneview. You have to enable the gizmo rendering at the top of the gameview is you want to see the line there. Also note that Debug.DrawLine will never show in a built game since (as already mentioned) it’s a debugging tool.