Wrong enemies die / disappear when shoot at one enemy

Hello all,

I have a very strange problem that I can not get solved. Maybe someone can help me?

In my game, enemies spawn constantly and want to be shot down. Now enemies are constantly despawning and I couldn`t not explain it. As a test, I have now let some enemies just run up and then shot at one.
Suddenly one disappeared behind me. I have shot 3 times at the same one again (because they have 3 health) and again another has disappeared.
Accordingly, “older” enemies seem to disappear first, as if they would die one after the other, no matter on which enemy I shoot.

Do you know this and can someone help?

Best regards, Nico.

Really? You’re telling me they WANT to be shot down?? Suicidal enemies?? :slight_smile:

There are a lot of ways this can happen. It might be you are using something generic like FindObjectOfType() to find an enemy, when in fact you should be using the thing you actually it. Or maybe you have a static reference somewhere.

Regardless, you must find a way to get the information you need in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3

Hi Kurt,

Thank you very much for your detailed help. Very cool :slight_smile:

I’m pretty sure I’m going to apply or try pretty much all of it and have the debug.logs output once in the console. After all, I learn from it too :slight_smile:

It`s my first game. Next time I can place these notes in advance.

Thanks for the link and the info.

Greetings, Nico.

1 Like

One thing that can be helpful is to give each enemy a different name.

Have a static variable in your enemy class:

static int counter;

And then in the Awake() (or Start()) method, do this:

counter++;
name = "Enemy #" + counter;

This will make each enemy have a different name (Enemy #1, Enemy #2, Enemy #3, etc.), which can help you track down which one is being destroyed by printing its name when you destroy it or when you hit it.

Yes I thought about something like that and reduced it to one enemy. But it didn`t changed a thing.

Hi, have you found a solution?

Are you having a similar issue? If so please post some code and info on your setup and we can help you problem solve it.