# Projectile not killing enemy--help debug code

**URL:** <https://forum.makecode.com/t/projectile-not-killing-enemy-help-debug-code/16387>\
**Category:** Help\
**Tags:** game\
**Created:** [November 17, 2022, 7:29pm UTC](https://forum.makecode.com/t/projectile-not-killing-enemy-help-debug-code/16387 "2022-11-17T19:29:06Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rng](https://avatars.discourse-cdn.com/v4/letter/r/54ee81/32.png) [@rng](https://forum.makecode.com/u/rng)\
**Post date:** [November 17, 2022, 7:29pm UTC](https://forum.makecode.com/t/projectile-not-killing-enemy-help-debug-code/16387/1 "2022-11-17T19:29:06Z")

</div>

Hi,

I’m sure this is a fairly simple question but this is my first time playing around with MakeCode and was wondering if someone felt like suggesting some fixes.

I’m trying to simply have my character kill the enemy with my projectile when hitting A. It seems like there’s possibly some sort of implicit order that MakeCode follows that I’m not aware of because the projectile killed enemies previously but then I moved around code and am not sure how I had things set up before. There were a few such instances, such as the game would end when I would have it set up to just lose a life until it hit 0 lives (then game over). I’ve attached the code and included a picture below. I removed that bit because it wasnt working just to try to debug other things.

 ![makecode arcade game](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/d/d158309952aa8dd477928df8e85864ae013436bb.png)

![arcade-HorseLips](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/1/10d3b19591344cbe67b37e9c4c844e0d303aefcd.png)

Thanks for the help!

---

<div class="post-metadata">

**Author:** ![richard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/richard/32/5417_2.png) [@richard](https://forum.makecode.com/u/richard)\
**Post date:** [November 17, 2022, 7:31pm UTC](https://forum.makecode.com/t/projectile-not-killing-enemy-help-debug-code/16387/2 "2022-11-17T19:31:29Z")

</div>

Ah, it looks like you’re using the global “enemySprite” variable in your events. When coding overlap events, you want to drag out the local variable from the event like so:

![drag-out-local](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/e/e808a5be0f06964d8c7ee159c980efeedfefb16f.gif)

The reason this was causing a bug was that every time a new enemy is created, the enemySprite variable is being updated to point to the new enemy. So every time you tried to destroy the enemy it would destroy whatever the most recently created one was instead of the one that was actually in the overlap event.

---

<div class="post-metadata">

**Author:** ![rng](https://avatars.discourse-cdn.com/v4/letter/r/54ee81/32.png) [@rng](https://forum.makecode.com/u/rng)\
**Post date:** [November 17, 2022, 9:42pm UTC](https://forum.makecode.com/t/projectile-not-killing-enemy-help-debug-code/16387/3 "2022-11-17T21:42:09Z")

</div>

Ah okay! That makes sense, thank you for the help.

I updated the blocks but neither the lives nor the projectiles are working properly. I attached the updated blocks/code here.  
 ![arcade-HorseLips2](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/c/cd1411968f29afe7c38760e22d31be3d3916a950.png)

---

<div class="post-metadata">

**Author:** ![KIKIvsIT](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/kikivsit/32/10492_2.png) [@KIKIvsIT](https://forum.makecode.com/u/KIKIvsIT)\
**Post date:** [November 17, 2022, 9:43pm UTC](https://forum.makecode.com/t/projectile-not-killing-enemy-help-debug-code/16387/4 "2022-11-17T21:43:00Z")

</div>

They seem to be working for me…what is it supposed to be doing that it’s not?

---

<div class="post-metadata">

**Author:** ![rng](https://avatars.discourse-cdn.com/v4/letter/r/54ee81/32.png) [@rng](https://forum.makecode.com/u/rng)\
**Post date:** [November 18, 2022, 5:36pm UTC](https://forum.makecode.com/t/projectile-not-killing-enemy-help-debug-code/16387/5 "2022-11-18T17:36:40Z")

</div>

Hmm, that’s interesting! Mine is still not killing the enemy when I hit it with a projectile and the lives counter isn’t working (I lose a life but the game ends instead of just counting down until I have 0 lives).

---

<div class="post-metadata">

**Author:** ![KIKIvsIT](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/kikivsit/32/10492_2.png) [@KIKIvsIT](https://forum.makecode.com/u/KIKIvsIT)\
**Post date:** [November 18, 2022, 9:58pm UTC](https://forum.makecode.com/t/projectile-not-killing-enemy-help-debug-code/16387/6 "2022-11-18T21:58:52Z")

</div>

Try opening the PNG in a private window. See if it works for you when you’re on a different browser.

---

<div class="post-metadata">

**Author:** ![daPokekid360](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/dapokekid360/32/9879_2.png) [@daPokekid360](https://forum.makecode.com/u/daPokekid360)\
**Post date:** [November 21, 2022, 1:18am UTC](https://forum.makecode.com/t/projectile-not-killing-enemy-help-debug-code/16387/7 "2022-11-21T01:18:23Z")

</div>

hello and welcome to the forums @rng

---

<div class="post-metadata">

**Author:** ![rng](https://avatars.discourse-cdn.com/v4/letter/r/54ee81/32.png) [@rng](https://forum.makecode.com/u/rng)\
**Post date:** [November 21, 2022, 1:19am UTC](https://forum.makecode.com/t/projectile-not-killing-enemy-help-debug-code/16387/8 "2022-11-21T01:19:19Z")

</div>

That fixed it! Thank you!!
