Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Tyler_Cox · Nov 15, 2020 at 12:13 PM · collisiontriggerdestroy

All enemies destroyed when only one gets hit.

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);
         }
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Zubzuke · Nov 13, 2020 at 09:55 PM

You should post the entire scripts. Also, to avoid bugs it should be

 collision.gameObject.GetComponent<EnemyHealth>().DealDamage(HeavyMissileDamage);
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tyler_Cox · Nov 13, 2020 at 10:13 PM 0
Share

I've posted the whole script and performed the change, but it did not solve the problem.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

228 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Collision not working for 2d platformer 2 Answers

Something wrong with destroying script 0 Answers

How to make two objects collide using "Is Trigger" 1 Answer

Collider to Delete Object 1 Answer

Why won't the collision work? 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges