- Home /
Destroy by Tag isnt working,Can't Destroy By Specific Tags
Hi!
I'm currently following a tutorial by GamesPlusJames that has a lot of things that don't work, i've found fixes for everything so far except this; I need to destroy game objects with the tag of 'Enemy' when the 2D box collider of the weapon and the 2D collider of the enemy meet and the code displayed isnt working. The code is attached, the enemies have the 'Enemy' tag and the sword is a trigger so as far as I can tell, everything is how it should be, its just not working. Thank you in advance!
Code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class HurtEnemy : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void onTriggerEnter2D (Collider2D other)
{
if (other.gameObject.tag == "Enemy")
{
Destroy(other.gameObject);
}
}
}
Answer by yummy81 · Feb 06, 2018 at 10:29 AM
you have to add Rigidbody2D component to your weapon or your enemy for this OnTriggerEnter2D method to work.
Both have rigidbody2d and it doesnt work, thank you though.
One day, I've got similar problem and in my case it was enough to replace:
if (other.gameObject.tag == "Enemy")
with:
if (other.gameObject.CompareTag("Enemy"))
Answer by ShadyProductions · Feb 06, 2018 at 11:08 AM
The actual problem is that you wrote
onTriggerEnter2D
instead of
OnTriggerEnter2D
Unity picks up the methods case sensitive through reflection.
Hello there,
@ShadyProductions above is right. Your method name should be "OnTriggerEnter2D" with a capital O, and not "onTriggerEnter2D".
Also, as a general rule of thumb, you should try putting Debug.Log() everywhere, and tell us which ones get triggered and which ones don't. That way we can deter$$anonymous$$e where the problem occurs, and help you more efficiently :).
Hope that helps!
Cheers,
~LegendBacon
Your answer
Follow this Question
Related Questions
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
Cannot destroy Component while GameObject is being activated or deactivated 2 Answers
Particle system not destroying. 3 Answers
How to only delete one of two collided objects? 1 Answer
GameObject.FindGameObjectsWithTag still finding destroyed object (C#) 1 Answer