Destroy GameObject or 2D collider does not work

I have a player prefab that I would like to destroy after it touches the collider of an enemy. The name of the enemy prefab is Enemy, and the name of the player prefab is Player (bland, I know…). I have this bit for the script that should destroy the character

using UnityEngine;
using System.Collections;

public class PlayerTrigger : MonoBehaviour
{
	void OnTriggerEnter2D(Collider2D col)
	{
		if (col.gameObject.tag == "Enemy") {
			Destroy (gameObject);
		}
	}
}

But when I run the game, nothing happens after the Player touches the Enemy. Am I doing something wrong?

1 Answer

1

You are saying that you have set the name of gameobjects, but in code you are comparing the tag of gameobjects

How can I fix it? I'm still learning how objects interact in Unity.