The enemies are destroyed with one projectile

So here is the case.

First of all i tried to get rid of NullReference when there are no enemies to track. So i changed the aimWeapon method:

   void aimWeapon()
    {
        if (target == null)
        {
            Attack(false);
        }
        else
        {
        float targetDistance = Vector3.Distance(transform.position,target.position);
        weapon.transform.LookAt(target);

            if(targetDistance < range)
            {
            Attack(true);
            }
            else
            {
                Attack(false);
            }
   }

Tested and the null references dropped.

But now, i see an another bug to fix.

In condition when there are a lot of towers and the enemy is not yet instantiated i see that enemies that get instatiated are destroyed with one projectile.
Here is a link to a video to illustrate the problem. On the 8th second the towers start to oneshot instantiated enemies.

I checked the HP of enemies everything looks good. Maybe the problem is that the enemy lacks time to get the hp parameter after instantiating?

I don’t know if this issue will be discussed in the next lecture, so decided to ask the question here.

Thx beforehand :slight_smile:

Hi Sergey,

Unfortunately, the link seems to be broken. I cannot access your video.

i see that enemies that get instatiated are destroyed with one projectile

Did you verify with Debug.Logs what you see? Maybe there are multiple projectiles that you cannot see. Analyse the moment when the enemy gets destroyed. How often do its methods get called? Who/what calls them?

Sorry, forgot to change the privacy of the video. The link is working now.

The amount of projectiles is accurate. There is no bug there.

To look closely into health i added this lines of code to EnemyHealth Script:

    void OnEnable() 
    {
        Debug.Log($"I am {gameObject.name} and i have {currentHP} HP");
    }

And here is what I’ve got

So i guess that enemies save their state after being disabled, if i understand this correctly

Also it is strange that there are some Ram(Clone)s with 0 hp right at the starters.

Indeed. That was my first questions after I saw your OnEnable method and the output in your console.

Your video looks odd, too. When the game starts, there seems to be an enemy at the starting position while the other enemies are moving. How many enemies are getting spawned at runtime? According to your Console, there seem to be dozens.

Also check which enemy your enemy spawner references. It must be a prefab from your Assets folder, not an enemy from your Hierarchy. And that prefab must be instantiated. If you reuse existing enemies, you have to reset their health. Otherwise, they keep their health value.

I changed text in OnEnable to have a № for the enemy.

So it looks like this now.

At the beginning of the game i do not have any Rams in the hierarchy, because i instantiate them from a prefab right from the assets.

So as you can see i get 5 enemies of each number in the beginning :face_with_raised_eyebrow:

Ok, i found out where i had made a mistake.

I had this

    void start() 
    {
        currentHP = maxHP;
    }

instead of this:

    void OnEnable() 
    {
        currentHP = maxHP;
    }

So the HP of enemies was set only once they appear on the scene. And since we have an ObjectPool the objects are pretty the same.

Good job on finding the issue! :slight_smile:

By the way, C# is case-sensitive. Unity’s Start method starts with a capital S.

For sure, I copy pasted the OnEnable method and then changed it to start while writing here)))

But thx for the note! :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms