Can't seem to get 2D colliders to trigger.

Hi guys.

I’m just running through a tutorial and have played around with this issue and simply can’t seem to figure it out.

It’s just a collision between a flying enemy object and a bullet or shot. If I turn off the ‘trigger’ value on the shot the two objects crash into each other and stop… meaning the rigidbody setting and colliders are working ok.

However, I’m trying to have the enemy take damage when it come in contact with the shot and it should also destroy both the enemy and the shot itself. Instead, if I turn on the trigger on the shot they just fly by each other and nothing actually happens.

Hopefully someone can pinpoint what I’m missing. Thanks guys.

This is the code for the ‘enemy’ object:

public class HealthScript : MonoBehaviour {

	// This declares a variable named hp. Used to define total hitpoints.
	public int hp = 1;

	// This declares a variable named isEnemy. It is used to determine if the object is an enemy or player.
	public bool isEnemy = true;

	// This function calls itself when another object enters the objects collider.
	// It uses the collider of the other object as a parameter.
	void OnTriggerEnter2d (Collider2D collider)
	{
		// This declares a ShotScript variable named shot.
		// The value of shot is determined by calling the other object and looking at it's parameters.
		// It checks to see if there's a 'ShotScript' script attached to it. If not the value of shot will be 'null'.
		ShotScript shot = collider.gameObject.GetComponent<ShotScript>();

		// This checks to see if 'shot' is null. This confirms if the object colliding is in fact a shot.
		if (shot != null)
		{
			// This checks if the shot is from the player or an enemy.
			if (shot.isEnemyShot != isEnemy)
			{
				// This subtracts the value of damage from the total hp.
				hp -= shot.damage;

				// This destroys the object if its hp hits zero.
				if (hp <= 0) 
				{
					Destroy (gameObject);
				}

				// This destroys the 'shot' object.
				Destroy(shot.gameObject);
			}
		}
	}
}

And this is the code for the ‘shot’ object:

public class ShotScript : MonoBehaviour {

	// This declares a variable named damage. Used to define damage inflicted to the player.
	public int damage = 1;

	// This declares a bool variable named isEnemyShot.
	// Used to determine if the projectile should damage other objects upon collision.
	public bool isEnemyShot = false;

	// This declares a variable named destroyTime. Used to define how long the shot should remain in the scene.
	public int destroyTime = 10;

	// Use this for initialization
	void Start () {

		// This is called everytime a shot is spawned.
		// It specifies that the object will be destroyed after 20 seconds.
		Destroy (gameObject, destroyTime);

}

Thanks again!!

I don’t see anything immediately wrong (assuming of course that you’ve set isEnemyShot = true somewhere), but I really think you should narrow down your point of failure. You could set some Debug.Log(“Hello Whirled”) messages at each stage of your event handler so that you know for sure whether you entered OnTriggerEnter2D at all, and whether you failed at the null test, or the isEnemyShot test.

The function name doesn’t have the correct case: OnTriggerEnter2D(), not OnTriggerEnter2d().