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?
Answer by aditya · Nov 19, 2016 at 07:02 AM
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.
Your answer
Follow this Question
Related Questions
connect 3 gameobjects 0 Answers
Getting nullReferenceException when respawning a gameObject and trying to access the audio source. 0 Answers
crouch attack collider 2d question 1 Answer
How To Add Multiple 2d Box Colliders on Same Object for Different Effects 0 Answers
Two colliders that trigger different things in the same object-hierarchy 0 Answers