- Home /
OnTriggerEnter2D doesnt work?
Hey guys. I have a very weird problem with my explosion that i made. The explosion works just fine with the player and one of the enemies, but it somehow doesnt work on the wall, even though the wall has the same Health script as the enemy. To explain a bit more: I declared a explosion radius which has the tag "explosion". Then i declared a health script in which the OnTriggerEnter2D-function is. So now the problem is that the explosion just works with one enemy and not with the wall even though they literally have the same OnTriggerEnter2D-function on them. Here is the healthScript: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Health : MonoBehaviour
{
public float maxHealth = 150f;
public float currentHealth;
public GameObject impactEffect;
public GameObject destructionEffect;
public bool isDead = false;
private void Start()
{
currentHealth = maxHealth;
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "Bullet")
{
Instantiate(impactEffect, transform.position, transform.rotation);
currentHealth -= 50f;
if (currentHealth <= 0)
{
isDead = true;
Destruction();
}
}
else if (collision.tag == "Explosion")
{
Instantiate(impactEffect, transform.position, transform.rotation);
currentHealth -= 200f;
if (currentHealth <= 0)
{
isDead = true;
Destruction();
}
}
}
private void Destruction()
{
Instantiate(destructionEffect, transform.position, transform.rotation);
Destroy(gameObject);
}
}
And heres a video that shows the problem: The Video Would be nice if someone could help :)
Answer by NaturedOne · Mar 05 at 05:44 AM
From your video it looked like you did not have your 2D box collider set as "is trigger"
Yes the explosive square itself should be an enemy you can touch and bump into, thats why its not set to "is Trigger". When the enemy dies however it instantiates a circle (which is invisible) which serves as the explosion radius. And that explosion radius is set to "is Trigger". I should have pointed that out more :)