Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Wrong enemies die / disappear when shoot at one enemy

Discussion in 'Scripting' started by Nizzo87, Sep 22, 2021.

  1. Nizzo87

    Nizzo87

    Joined:
    Dec 28, 2019
    Posts:
    7
    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.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,517
    Really? You're telling me they WANT to be shot down?? Suicidal enemies?? :)

    There are a lot of ways this can happen. It might be you are using something generic like FindObjectOfType<T>() 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://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494
     
  3. Nizzo87

    Nizzo87

    Joined:
    Dec 28, 2019
    Posts:
    7
    Hi Kurt,

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

    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 :)

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

    Thanks for the link and the info.

    Greetings, Nico.
     
    Kurt-Dekker likes this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,517
    One thing that can be helpful is to give each enemy a different name.

    Have a static variable in your enemy class:

    Code (csharp):
    1. static int counter;
    And then in the Awake() (or Start()) method, do this:

    Code (csharp):
    1. counter++;
    2. 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.
     
  5. Nizzo87

    Nizzo87

    Joined:
    Dec 28, 2019
    Posts:
    7
    Yes I thought about something like that and reduced it to one enemy. But it didn`t changed a thing.
     
  6. xxadonexx_unity

    xxadonexx_unity

    Joined:
    Jan 21, 2023
    Posts:
    1
    Hi, have you found a solution?
     
  7. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    494
    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.