Turrets keep shooting when all enemies destroyed

When the enemy reaches the end of the path, it doesn’t destroy itself, but the player still loses lives and the turrets can still shoot them.
The enemies do get destroyed when they receive enough damage but not when the reach the end, and I’m not sure why.

image
image
image

here is my code for enemy mover


and also whenever all the enemies are destroyed, the turrets proceed to keep shooting at nothing.
image

here is my tower scripts


Hi,

Please note, it’s better to copy/paste your code and apply the code fencing characters, rather than using screenshots. Screenshots are ideal for displaying specific details from within a game engine editor or even error messages, but for code, they tend to be less readable, especially on mobile devices which can require extensive zooming and scrolling.

You also prevent those that may offer to help you the ability to copy/paste part of your code back to you with suggestions and/or corrections, meaning that they would need to type a potentially lengthy response. You will often find that people are more likely to respond to your questions if you make it as easy as possible for them to do so.

Regarding your problem, have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture? Have you already tried to add Debug.Logs to your code to see what is going on during runtime?

Hope this helps :slight_smile:


See also;

Sorry, I’ll make sure to copy+paste my code in the future, I managed to get the object to destroy itself once it gets to end again but I can’t find out how to stop the turrets from continually shooting.
I can’t find what part of which video he goes through it if in any of them.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TargetLocator : MonoBehaviour
{
    [SerializeField] float range = 20f;
    [SerializeField] ParticleSystem projectileParticles;
    [SerializeField] Transform weapon;
    Transform target;


    void Awake()
    {
        Attack(false);
    }

    void Update()
    {
        FindClosestTarget();
        AimWeapon();
    }


    void FindClosestTarget()
    {
        Enemy[] enemies = FindObjectsOfType<Enemy>(); // finds entities with the enemy script
        Transform closestTarget = null;
        float maxDistance = Mathf.Infinity;

        foreach(Enemy enemy in enemies)
        {
            float targetDistance = Vector3.Distance(transform.position, enemy.transform.position); // compares distance from target and turret
        
            if(targetDistance < maxDistance)
            {
                closestTarget = enemy.transform;
                maxDistance = targetDistance;
            }
        
        }

        target = closestTarget;
    }

    void AimWeapon()
    {
        float targetDistance = Vector3.Distance(transform.position, target.position);

        

        if(targetDistance < range)
        {
            weapon.LookAt(target);
            Attack(true);
        }
        else
        {
            Attack(false);
            Debug.Log("Object destroyed");
        }

    }

    void Attack(bool isActive)
    {
        var emissionModule = projectileParticles.emission;
        emissionModule.enabled = isActive;
    }

}

this is in the TargetLocator script.
It’s difficult to look through the scripts in the video since I’ve used different key words because I’m trying to make my game a bit more original.
Any help is appreciated though, thanks!

Is this a different problem from the one described in your inital post? In this video, we do not destroy the enemy when it reached the end of the path. Instead, we disable it. See Gary’s code.

In my code I’d like to destroy it since it’s easier for me to understand, so that part is fine and resolved.
I’m just not sure how to stop the particles from firing once all targets have been destroyed

What does “all targets” mean? Where is “all targets” defined in your code? If you know that, you could solve your remaining problem with an if-statement.

By “all targets” I mean when nothing with the “enemy” script is present on screen, the gun stops firing but I don’t know how to code that.
I’m not great at this and am still learning and would appreciate if you could show me how to do so.
The bug only seems to occur when the final target is destroyed and there’s nothing left for them to shoot at. If a target goes out of range it stops shooting or if it is destroyed but there is still a target somewhere on the map, it will also stop shooting.

One way to solve this problem would be to parent each spawned game object to one “Enemies” game object. Then you could check the childCount of the “Enemies” game object.

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

Privacy & Terms