- Home /
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
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!
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
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!!!