1
\$\begingroup\$

I am trying to implement a new attack into my project. I can get the projectile to spawn and deal damage to an enemy, however, once it deals damage, every enemy gets destroyed instead of just the one that was hit. I am not using any static variables and I cannot figure out why this issue is happening.

Here is the projectile script:

 public GameObject HeavyExplosion;
 public GameObject LightExplosion;
 public float HeavyMissileSpeed = 1;
 public float LightMissileSpeed = 1;
 public float HeavyLaserSpeed;
 public float LightLaserSpeed;
 public float HeavyBulletSpeed;
 public float LightBulletSpeed;
 Rigidbody RB;
 public int HeavyMissileDamage;
 public int LightMissileDamage;
 public int HeavyLaserDamage;
 public int LightLaserDamage;
 public int HeavyBulletDamage;
 public int LightBulletDamage;
 public float ProjectileTimeout;
 float TimeElapsed;
 bool HasExploded;
 void Start()
 {
     RB = gameObject.GetComponent<Rigidbody>();
     HasExploded = false;
 }
 void Update()
 {
     TimeElapsed += Time.deltaTime;
     if(TimeElapsed >= ProjectileTimeout)
     {
         Destroy(gameObject);
     }
 }
 void FixedUpdate()
 {
     if (gameObject.CompareTag("HeavyMissile") && !HasExploded)
     {
         RB.velocity = HeavyMissileSpeed * gameObject.transform.forward;
     }
     if (gameObject.CompareTag("LightMissile") && !HasExploded)
     {
         RB.velocity = LightMissileSpeed * gameObject.transform.forward;
     }
     if (gameObject.CompareTag("HeavyLaser"))
     {
         RB.velocity = HeavyLaserSpeed * gameObject.transform.forward;
     }
     if (gameObject.CompareTag("LightLaser"))
     {
         RB.velocity = LightLaserSpeed * gameObject.transform.forward;
     }
     if (gameObject.CompareTag("HeavyBullet"))
     {
         RB.velocity = HeavyBulletSpeed * gameObject.transform.forward;
     }
     if (gameObject.CompareTag("LightBullet"))
     {
         RB.velocity = LightBulletSpeed * gameObject.transform.forward;
     }
 }
 private void OnCollisionEnter(Collision collision)
 {
     if (collision.gameObject.CompareTag("Enemy") && gameObject.CompareTag("HeavyMissile"))
     {
         collision.transform.parent.GetComponent<EnemyHealth>().DealDamage(HeavyMissileDamage);
         GetComponent<MeshRenderer>().enabled = false;
         GetComponent<Collider>().enabled = false;
         RB.velocity = new Vector3(0, 0, 0);
         Instantiate(HeavyExplosion, transform);
         HasExploded = true;
         Destroy(gameObject, 1);
     }
     else if (collision.gameObject.CompareTag("Enemy") && gameObject.CompareTag("LightMissile"))
     {
         collision.transform.parent.GetComponent<EnemyHealth>().DealDamage(LightMissileDamage);
         GetComponent<MeshRenderer>().enabled = false;
         GetComponent<Collider>().enabled = false;
         RB.velocity = new Vector3(0, 0, 0);
         Instantiate(LightExplosion, transform);
         HasExploded = true;
         Destroy(gameObject, 1);
     }
     else if (collision.gameObject.CompareTag("Enemy") && gameObject.CompareTag("HeavyLaser"))
     {
         collision.transform.parent.GetComponent<EnemyHealth>().DealDamage(HeavyLaserDamage);
         Destroy(gameObject);
     }
     else if (collision.gameObject.CompareTag("Enemy") && gameObject.CompareTag("LightLaser"))
     {
         collision.transform.parent.GetComponent<EnemyHealth>().DealDamage(LightLaserDamage);
         Destroy(gameObject);
     }
     else if (collision.gameObject.CompareTag("Enemy") && gameObject.CompareTag("HeavyBullet"))
     {
         collision.transform.parent.GetComponent<EnemyHealth>().DealDamage(HeavyBulletDamage);
         Destroy(gameObject);
     }
     else if (collision.gameObject.CompareTag("Enemy") && gameObject.CompareTag("LightBullet"))
     {
         collision.transform.parent.GetComponent<EnemyHealth>().DealDamage(LightBulletDamage);
         Destroy(gameObject);
     }
     else
     {
         Destroy(gameObject);
     }
 }
 private void OnTriggerEnter(Collider other)
 {
     if (other.CompareTag("Enemy") && gameObject.CompareTag("HeavyMissile"))
     {
         other.transform.parent.GetComponent<EnemyHealth>().DealDamage(HeavyMissileDamage);
         GetComponent<MeshRenderer>().enabled = false;
         GetComponent<Collider>().enabled = false;
         RB.velocity = new Vector3(0, 0, 0);
         Instantiate(HeavyExplosion, transform);
         HasExploded = true;
         Destroy(gameObject, 1);
     }
     else if (other.CompareTag("Enemy") && gameObject.CompareTag("LightMissile"))
     {
         other.transform.parent.GetComponent<EnemyHealth>().DealDamage(LightMissileDamage);
         GetComponent<MeshRenderer>().enabled = false;
         GetComponent<Collider>().enabled = false;
         RB.velocity = new Vector3(0, 0, 0);
         Instantiate(LightExplosion, transform);
         HasExploded = true;
         Destroy(gameObject, 1);
     }
     else if (other.CompareTag("Enemy") && gameObject.CompareTag("HeavyLaser"))
     {
         other.transform.parent.GetComponent<EnemyHealth>().DealDamage(HeavyLaserDamage);
         Destroy(gameObject);
     }
     else if (other.CompareTag("Enemy") && gameObject.CompareTag("LightLaser"))
     {
         other.transform.parent.GetComponent<EnemyHealth>().DealDamage(LightLaserDamage);
         Destroy(gameObject);
     }
     else if (other.CompareTag("Enemy") && gameObject.CompareTag("HeavyBullet"))
     {
         other.transform.parent.GetComponent<EnemyHealth>().DealDamage(HeavyBulletDamage);
         Destroy(gameObject);
     }
     else if (other.CompareTag("Enemy") && gameObject.CompareTag("LightBullet"))
     {
         other.transform.parent.GetComponent<EnemyHealth>().DealDamage(LightBulletDamage);
         Destroy(gameObject);
     }
 }

Here is the enemy health script:

     public int MaxHealth = 1;
     int HP;
 
     public GameObject Explosion;
     public EnemySpawnerEternal SpawnReference { get; set; }
 
     void Awake()
     {
         HP = MaxHealth;
     }
 
     public void DealDamage(int Damage)
     {
         HP -= Damage;
         if(HP <= 0)
         {
             if(SpawnReference != null)
             {
                 SpawnReference.ResartSpawner();
             }
             GameObject.Instantiate(Explosion, transform.position, Quaternion.identity);
             Destroy(gameObject);
         }
     }
 }
\$\endgroup\$
4
  • \$\begingroup\$ Which of these scripts is on which game objects? \$\endgroup\$ – Philipp Nov 24 '20 at 21:49
  • \$\begingroup\$ @Ceriac When you include full classes in your question, it makes the question a little easier to read and discuss when you include the class headers, e.g. public class EnemyHealth : MonoBehaviour { \$\endgroup\$ – Kevin Nov 24 '20 at 22:17
  • 1
    \$\begingroup\$ Unrelated to your question, you can simplify this code by a factor of six or more by not storing every possible speed/damage value on each projectile. Just store the value for that one projectile type. Then you can spawn a different prefab depending on the projectile you want, with that one damage variable set to the appropriate amount for that type. \$\endgroup\$ – DMGregory Nov 24 '20 at 22:48
  • \$\begingroup\$ @Philipp - The projectile behavior script is on the projectiles that get spawned in, and the enemy health script is on all enemies. \$\endgroup\$ – Ceriac Nov 24 '20 at 23:04
1
\$\begingroup\$

This is a bit of a guess, but based on my experience with pooling:

You have this line in your enemy health script:

SpawnReference.ResartSpawner();

I'm guessing that EnemySpawnerEternal is some type of spawn-pool system. Perhaps calling RestartSpawner() returns all spawned objects to the pool?

\$\endgroup\$
2
  • \$\begingroup\$ There is another attack that the player can perform that doesn't cause this issue. That script uses CompareTag("Enemy") and other.transform.parent.GetComponent<EnemyHealth>().DealDamage() and doesn't cause this issue. \$\endgroup\$ – Ceriac Nov 24 '20 at 23:06
  • \$\begingroup\$ @Ceriac Did you try commenting out the line of code that restarts the spawner, just to be sure? \$\endgroup\$ – Kevin Nov 25 '20 at 0:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.