Skip to content

Instantly share code, notes, and snippets.

@sabotai
Last active May 10, 2016 21:41
Show Gist options
  • Select an option

  • Save sabotai/f75018ff464c0a86ea1b723a416c66c6 to your computer and use it in GitHub Desktop.

Select an option

Save sabotai/f75018ff464c0a86ea1b723a416c66c6 to your computer and use it in GitHub Desktop.
Unity destroy upon collision 2D
using UnityEngine;
using System.Collections;
public class DestroyCollision : MonoBehaviour { //in the project tab of Unity, make a new "C# Script" called "DestroyCollision"
public string tagName; //this will show up as an input box for the component, which can be given a tag of your choice. assign the
void OnCollisionEnter2D(Collision2D myCol) { //run the following whenever a collision happens to the object attached with this script
Debug.Log("Collision with " + myCol.gameObject);
if (myCol.gameObject.tag == tagName) { //does the object collided with have the tag in question?
Destroy(myCol.gameObject); //if so, destroy that object!
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment