Collider2D and object destroy

Hey
I don’t know how to make a reference in code to an object I created. For example, the object is named “enemy”
and I’d like to object Sword destroy only this.
This is a code of a Sword class:

using UnityEngine;
using System.Collections;

public class Sword : MonoBehaviour {

void OnTriggerEnter2D(Collider2D other)
    {
        Destroy (other.gameObject);
    }
}

For now, this code deletes all objects in the same Layer…

Please, help me!:slight_smile:

1 Like
  1. You could manage this by layers.
  2. You could just chceck object name like below:
if(other.name== "Enemy")
{
     Destroy(other.gameObject);
}
2 Likes

Thank you! That was exactly what I needed (point 2.), but the first solution is helpful as well.

1 Like