I think what might happen is that it attacks a target. When your target is doing a dying animation, it looks for the next target to attack and attacks the next target when such a target is found. The previous target will drop loot after your next target is already chosen, and thus the goblinPosition variable is already overwritten by the new target's position, therefore the if-statement in your loot() method will not be satisfied. The first thing and (to me) most obvious thing to recommend is that you wait until your target has dropped the loot, but there are also other ways to make it work.