- Home /
Collider Tag not working for enemy
I'm having a problem with letting the bullet notice it is an Enemy. This only happens when I try to use an explosionRadius. The enemy Prefab is correctly tagged as "Enemy", but it still won't recognize it as an "Enemy". I don't know how to fix it, I've tried numerous methods, but none worked.
See pictures and code to see what I mean.
[code=Csharp]
//Called when the bullet reaches a target
void HitTarget() {
GameObject effectInstance = (GameObject)Instantiate(BulletImpactEffect, transform.position, transform.rotation);
Destroy(effectInstance,2f);
//If this bullet has an explosionRadius > 0 then it calls the explode function (This works for the missile)
if (explosionRadius > 0f) {
Explode();
} else {
Damage(target);
}
Destroy(gameObject);
}
//Explode function
void Explode() {
//Scans for each object in the radius of the explosion, if the object is tagged as "Enemy", Then it calls the damage function
Collider[] colliders = Physics.OverlapSphere(transform.position,explosionRadius);
foreach (Collider collider in colliders) {
// Here is the problem. If I try != "Enemy" Then it removes all the objects that are not tagged as "Enemy". But when it is tagged it does nothing to the enemy?
if (collider.tag == "Enemy") {
Damage(collider.transform);
}
}
}
//For the moment it just destroys the gameObject
void Damage(Transform enemy) {
Destroy(enemy.gameObject);
}
[/code]
Answer by Bunny83 · Jun 17, 2019 at 05:05 PM
There doesn't seem to be any collider on your enemy gameobject. In your code you directly destroy the object the collider is attached to. If you have collider(s) as child object(s) your code would only destroy those colliders and not the parent object.
Since your enemies seem to have a script called Enemy on the root, the easiest solution would be to do:
foreach (Collider collider in colliders)
{
if (collider == null)
continue;
Enemy e = collider.GetComponentInParent<Enemy>();
if (e == null)
continue;
Damage(e.transform);
}
Note that the first check if collider is null might not be necessary. However if one enemy has several child colliders you would need it as if two or more colliders of the same enemy are inside the radius the enemy might have been destroyed already when it processes the second collider in the list.
Answer by SirPaddow · Jun 17, 2019 at 05:03 PM
I see by your printscreen that the collider is not on this gameobject. Check that the gameObject containing the collider is also tagged "enemy", or from your collider, (line 26), try to find the gameobject that you named "Enemy" (the one shown in your printscreen)
Answer by camdenlink7 · Jun 17, 2019 at 03:42 PM
Hey, I don't know if this will help or not, but try to change the Damage function to take a game object instead of a transform. Then do Damage(collider.gameObject); I do not know if that will work or not, I have not tested it, just a thought. After that change it to Destroy(enemy);
I tried the change, but dind't work. For some reason it doesn't enter the if statement: if(collider.tag == "Enemy"){...}
Feels like it doesn't recognize or know the tag "Enemy", but in the picture it is clearly tagged as "Enemy".
Your answer
![](https://koobas.hobune.stream/wayback/20220612203348im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Enemy fall apart on projectile impact 0 Answers
Fist Punch Collision 0 Answers
How can I have the player be able to kill an enemy by jumping on top of it? 1 Answer
Multiple Cars not working 1 Answer