# hero.findNearest(units) method

**URL:** <https://discourse.codecombat.com/t/hero-findnearest-units-method/14869>\
**Category:** Adventurer\
**Created:** [May 31, 2018, 4:56pm UTC](https://discourse.codecombat.com/t/hero-findnearest-units-method/14869 "2018-05-31T16:56:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xarpe\_Serpe](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/xarpe_serpe/32/8235_2.png) [@Xarpe\_Serpe](https://discourse.codecombat.com/u/Xarpe_Serpe)\
**Post date:** [May 31, 2018, 4:56pm UTC](https://discourse.codecombat.com/t/hero-findnearest-units-method/14869/1 "2018-05-31T16:56:49Z")

</div>

I try to optimize the next movement.  
First move to gold, them silver, and bronze.  
But the program breaks at first gold.  
It said there is a error in hero.moveXY(gold.pos.x, gold.pos.y)

I think method this.findNearest(units) can not work with this.findItems()

Thanks a lot.

golds = []  
silvers = []

while True:  
# Find coins and/or attack the enemy.  
# Use flags and your special moves to win!  
items = hero.findItems()  
for item in items:  
if item.value == 3:  
golds.append = item  
elif item.value == 2:  
silvers.append = item  
else:  
pass

```
if golds:
    hero.say("Oro")
    gold = hero.findNearest(golds)
    hero.moveXY( **gold.pos**.x, gold.pos.y)
elif silvers:
    hero.say("Plata")
    silver = hero.findNearest(silvers)
    hero.moveXY(silver.pos.x, silver.pos.y)
else:
    hero.say("Bronce")
    item = hero.findNearestItem()
    hero.moveXY(item.pos.x, item.pos.y)
```

---

<div class="post-metadata">

**Author:** ![Gabriel\_Dupuis](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/gabriel_dupuis/32/7450_2.png) [@Gabriel\_Dupuis](https://discourse.codecombat.com/u/Gabriel_Dupuis)\
**Post date:** [May 31, 2018, 5:22pm UTC](https://discourse.codecombat.com/t/hero-findnearest-units-method/14869/2 "2018-05-31T17:22:20Z")

</div>

You are not using append function correctly  
golds.append(item)  
silvers.append(item)

---

<div class="post-metadata">

**Author:** ![Gabriel\_Dupuis](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/gabriel_dupuis/32/7450_2.png) [@Gabriel\_Dupuis](https://discourse.codecombat.com/u/Gabriel_Dupuis)\
**Post date:** [May 31, 2018, 11:38pm UTC](https://discourse.codecombat.com/t/hero-findnearest-units-method/14869/3 "2018-05-31T23:38:55Z")

</div>

btw for your own knowledge if you say item = golds.append

you can use item(item) to append the array golds

You are saying that the object + function is equal a new function item
