Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 politdejan8 · Nov 23, 2019 at 02:29 PM · collisiongameobjectprefab2d gameenemy health

Destroy a specific prefab (GameObject) Unity2D

Hello! I'm making a rogue-like game like "The Binding of Isaac: Rebirth". I'm making changes on the gameplay and there comes my problem. The enemies spawn randomly and they follows / shoots the player. They spawn with a script that Instantiate on a random position an enemy and makes a "Enemy(Clone)" from a prefab.

So... I want to destroy the prefab that I'm shooting with my player. I have 2 scripts, one for the bullets and other for the Enemy. When the bullet collides with an Enemy ALWAYS I destroy the first cloned GameObject instead wich I'm shooting alt text

I need that the GameObject destroyed will be wich 'im shooting. Here are my scripts, sorry if are bad I'm noobie yet: Enemy.cs:

 public class Enemy : MonoBehaviour
 {
     public float enemyHelth = 100;
 
     public void TakeDamage(float amount)
     {
         enemyHelth -= amount;
     
         if (enemyHelth <= 100)
         {
             Die();
         }
     }
 
     void Die()
     {
         Destroy(gameObject);
     }
 
 }


Bullet.cs :

 public class Bullet : MonoBehaviour
 {
     
     public float damage = 100f; //Daño que hace la bala.
     private float delayAnim = 0.3f; //Tiempo que dejamos que este activa la animación.
     public GameObject hitEffect; //Sitio donde poner el efecto de impacto de la bala.
     
 
 
     private void OnCollisionEnter2D(Collision2D collision)
     {
         GameObject enemyGo = GameObject.FindGameObjectWithTag("Enemy"); //Busca al enemigo mediante su etiqueta.
         Enemy enemy = enemyGo.GetComponent<Enemy>(); //Busca el componente Enemy dentro del GameObject enemyGo.
         
         if(enemy != null) //Detecta si ha encontrado a un objecto con el script Enemy.
         {
             enemy.TakeDamage(damage); //Hace daño al enemigo. (Ver script Enemy.cs)
         }
 
         GameObject effect = Instantiate(hitEffect, transform.position, Quaternion.identity);
         Destroy(effect, delayAnim);
         Destroy(gameObject);
     }
 
 
 }

Sorry again about the comments, my native Language is Spanish. Thank you all!

unity-201918f1-personal-samplesceneunity-project-j.gif (419.0 kB)
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
2
Best Answer

Answer by HenriMR · Nov 23, 2019 at 04:31 PM

In your OnCollisionEnter, your are using GameObject.FindGameObjectWithTag, which searches all game object in other. So it will find first the enemy you instantiated first.

So should do something like this:

 private void OnCollisionEnter2D(Collision2D collision)
      {
          GameObject enemyGo = collision.otherCollider.gameObject;
 
           if (enemyGo.transform.tag == "Enemy")
           {
                             Enemy enemy = enemyGo.GetComponent<Enemy>(); //Busca el componente Enemy dentro del GameObject enemyGo.
          
                      if(enemy != null) //Detecta si ha encontrado a un objecto con el script Enemy.
                    {
                                 enemy.TakeDamage(damage); //Hace daño al enemigo. (Ver script Enemy.cs)
                     }
  
                    GameObject effect = Instantiate(hitEffect, transform.position, Quaternion.identity);
                    Destroy(effect, delayAnim);
                    Destroy(gameObject);
 
            }
 
 
      }
  
  
  }

This links might help you:

https://docs.unity3d.com/ScriptReference/Collision2D.html

https://docs.unity3d.com/ScriptReference/Collider2D.OnCollisionEnter2D.html

Comment
Add comment · Show 2 · 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 politdejan8 · Nov 23, 2019 at 04:56 PM 0
Share

At first I didn't be able make it works with: GameObject enemyGo = collision.otherCollider.gameObject; because it didn't entered in the if statement.

So I changed seeying yours links posted above and now with this: GameObject enemyGo = collision.transform.gameObject; works like a charm!

Thank you so much!!!

avatar image HenriMR politdejan8 · Nov 23, 2019 at 05:03 PM 1
Share

Happy I could help :)

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

261 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 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 Detection for a Prefab? 3 Answers

Destroying a prefab on collision with a cube? 1 Answer

continous movement for rigidbody2d using addforce with buttons 2 Answers

If two of the same objects spawn on top of each other, is it possible to destroy only one? 0 Answers

Make isKinematic false when touching a Collider? 1 Answer


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