# Question about distanceTo

**URL:** <https://discourse.codecombat.com/t/question-about-distanceto/25691>\
**Category:** Level Help\
**Created:** [November 27, 2020, 4:58pm UTC](https://discourse.codecombat.com/t/question-about-distanceto/25691 "2020-11-27T16:58:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![milton.jinich](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/milton.jinich/32/21419_2.png) [@milton.jinich](https://discourse.codecombat.com/u/milton.jinich)\
**Post date:** [November 27, 2020, 4:58pm UTC](https://discourse.codecombat.com/t/question-about-distanceto/25691/1 "2020-11-27T16:58:49Z")

</div>

so I keep trying to do distanceTo but this happens

 ![Screen Shot 2020-11-27 at 11.57.55 AM](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/3X/a/2/a2ad8590d6bf10db4bd089e0b7b8d8d523b9582b.jpeg)  
I have been doing it like this for a while but it isn’t working now

---

<div class="post-metadata">

**Author:** ![FalconX11\_312](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/falconx11_312/32/26330_2.png) [@FalconX11\_312](https://discourse.codecombat.com/u/FalconX11_312)\
**Post date:** [November 27, 2020, 4:59pm UTC](https://discourse.codecombat.com/t/question-about-distanceto/25691/2 "2020-11-27T16:59:58Z")

</div>

Ah you need to define enemy right on the line above the line marked by the error. It happens to me too

---

<div class="post-metadata">

**Author:** ![Deadpool198](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/deadpool198/32/16753_2.png) [@Deadpool198](https://discourse.codecombat.com/u/Deadpool198)\
**Post date:** [November 27, 2020, 5:03pm UTC](https://discourse.codecombat.com/t/question-about-distanceto/25691/3 "2020-11-27T17:03:58Z")

</div>

That may work in some cases, but really you should follow the duck’s instructions: use if. How can you work out the distance to an enemy if there is no enemy? There is an enemy on the screen now but I bet the error occurred when there were no enemies… hero.distanceTo() does not have a built in conditional statement, it doesn’t find the distance if there is an enemy, it just tries to find the distance even when there isn’t an enemy, which causes the error.  
Danny

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/a/a9445509d1a758c41b5ddcddc8c8e20a5dedb67f.png) [@system](https://discourse.codecombat.com/u/system)\
**Post date:** [November 28, 2020, 5:03am UTC](https://discourse.codecombat.com/t/question-about-distanceto/25691/4 "2020-11-28T05:03:59Z")

</div>

Closed on November 28, 2020, 5:03am UTC

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.
